selenium2 and webdriver Alert
来源:互联网 发布:苹果macbook必备软件 编辑:程序博客网 时间:2024/05/20 02:53
driver.findElement(By.xpath("//input[@value='添加']")).click(); //Pops out an Alert and program stops, does not continue
how to click the alert?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~啊啊啊啊 怎么没有人呢? (TRANS: ahahahahaha why there is no one here to reply my post?)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~我顶 (TRANS: Let me promote this post!)
As of the latest selenium 2 release, this can be done (at least using the FirefoxDriver):
driver.switchTo().alert().accept();
In previous version of Selenium 2 I have had no choice that to handle alerts in Internet Explorer by overriding the window.alert in Javascript:
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;// Override window.alert to store the prompt and accept it automaticallyjs.ExecuteScript("window.alert = function(msg) { document.bAlert = true; document.lastAlert=msg; }");// Do some stuff...// Check for alertObject o = js.ExecuteScript("return document.bAlert");if (o != null && (bool)o == true){ //retrieve the alert message o = js.ExecuteScript("return document.lastAlert"); // Do something with the alert text}
Selenium 2.0b3 has support for handling Alerts in IE and Firefox, so you can do the following:
IAlert alert = driver.SwitchTo().Alert();// Get the text from the alertstring alertText = alert.Text;// Accept the alertalert.Accept();
However, I have not been able to get the above to work with Yes/No alerts (Dismiss() works for No but Accept() doesn't work for Yes). I'm in the process of looking at the IEDriver to work out why this is.
In Selenium 2, currently alerts are only handled in the Firefox browser. You don't specify what language you're using for your tests, but here's how to handle an alert using ruby. (This is taken from the Ruby Bindings page on the selenium wiki).
Javascript Alert/Confirm
You can use webdriver to handle javascript alert and confirm dialogs. The implementation for both is the same.
Note: At this time the API is only available in Firefox (or in Firefox using the remote server), and only alert/confirms that are generated post load can be captured.
require "selenium-webdriver"driver = Selenium::WebDriver.for :firefoxdriver.navigate.to "http://mysite.com/page_with_alert.html"driver.find_element(:name, 'element_with_alert_javascript').clicka = driver.switch_to.alertif a.text == 'A value you are looking for' a.dismisselse a.acceptend
C# code:
IAlert alert = driver.SwitchTo().Alert();alert.Accept(); System.Threading.Thread.Sleep(milliseconds);
IWebDriver driver = new ChromeDriver();driver.Navigate().GoToUrl("http://skynet:8081/1.htm");var selenium = new WebDriverBackedSelenium(driver, driver.Url);selenium.Start();selenium.Click("css=input[type=button]");Assert.AreEqual(selenium.GetConfirmation(), "Are you sure?");Assert.AreEqual("OK", selenium.GetAlert());// <input type="button" value="Alert test" />driver.Quit();
Alert alert = driver.switchTo().alert();alert.accept();
If you want to cancel the pop up use the following instead of alert.accept(): alert.dismiss();
You will have to handle exception and run your handler code for Alert, for Java:
try{ driver.findElement(By.xpath("//input[@value='添加']")).click(); } catch(org.openqa.selenium.UnhandledAlertException e){ Alert alert = driver.switchTo().alert(); alert.accept(); // you logic here what you want to do next }
Catch this exception, and then you can accordingly accept or reject alert.
- selenium2 and webdriver Alert
- Selenium2 WebDriver入门
- Selenium2.0之WebDriver
- selenium2.0(WebDriver) API
- selenium2(WebDriver) API
- selenium2(WebDriver) API
- selenium2(webdriver)的“兄弟姐妹”
- selenium2 webdriver 简单使用
- selenium2(WebDriver) API
- selenium2 webdriver 简单使用
- selenium2(WebDriver) API
- Selenium2.0 WebDriver入门指南
- Selenium2.0 WebDriver入门指南
- Selenium2.0 WebDriver入门指南
- Selenium2.0 WebDriver基本操作
- Selenium2.0 WebDriver入门指南
- Selenium2.0 WebDriver入门指南
- Selenium2(WebDriver)总结(常用类)
- 六十四、基础框架(二十二)文件操作-基本文件操作
- Activity生命周期详解
- 亲。。。。。。我不存在!
- 信息检索和网络数据挖掘领域论文技术基础
- 自定义水平的进度条(效果很炫)ProgressBar
- selenium2 and webdriver Alert
- Spring 框架 IOC本质
- 六十五、基础框架(二十三)文件操作-数据物件
- CVPR2012文章整理
- Linux shell中的标准输入、输出重定向
- 问:两个小组独立地测试同一个程序,求估计程序中的错误总数
- Android 中 ListView 分页加载数据
- 设置硬盘AHCI模式引起的XP提示找到PCI Device设备问题
- [SGU]120. Archipelago