我有以下Selenium脚本可在rediff上打开警报:
I have the following Selenium script for opening alert on rediff:
public class TestC { public static void main(String[] args) throws InterruptedException, Exception { System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("www.rediff/"); driver.findElement(By.xpath("//*[@id='signin_info']/a[1]")).click(); driver.findElement(By.id("btn_login")).click(); Thread.sleep(5000); Alert alert=driver.switchTo().alert(); alert.accept(); } }该脚本在Firefox和IE9中运行正常,但是在打开警报后使用Google Chrome浏览器,其余代码无法正常工作.最主要的是不会显示任何异常,错误或任何内容.
This very same script is working fine in Firefox and IE9, however using Google Chrome after opening the alert, rest of the code is not working. The main thing is that does not shows any exception, error or anything.
请尽快提供任何解决方案. 非常感谢!
Please provide any solution as soon as possible. Thanks a lot!
注意:如果我们需要更改浏览器的任何设置或任何其他内容,请告诉我.
Note: If we need to change any setting of browser or any thing please let me know.
Selenium version:Selenium(2) Webdriver OS:Windows 7 Browser:Chrome Browser version:26.0.1410.64 m推荐答案
我很确定您的问题很常见,这就是为什么我从不建议使用Thread.sleep()的原因,因为它不能保证代码只能运行当Alert出现时,即使显示警报,它也可能会增加测试时间.
I'm pretty sure your problem is a very common one, that's why i never advise using Thread.sleep(), since it does not guarantee the code will run only when the Alert shows up, also it may add up time to your tests even when the alert is shown.
下面的代码应该只等到页面上显示一些警报后,才建议您也使用这一个Firefox和IE9.
The code below should wait only until some alert is display on the page, and i'd advise you using this one Firefox and IE9 aswell.
public class TestC { public static void main(String[] args) throws InterruptedException, Exception { System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe"); WebDriver driver = new ChromeDriver(); WebDriverWait wait = new WebDriverWait(driver, 5); driver.get("www.rediff/"); driver.findElement(By.xpath("//*[@id='signin_info']/a[1]")).click(); driver.findElement(By.id("btn_login")).click(); wait.until(ExpectedConditions.alertIsPresent()); Alert alert = driver.switchTo().alert(); alert.accept(); } }这里所做的大部分工作都是更改Thread.sleep(),因为这种情况实际上只会在页面中出现alert()时才在代码上向前移动.一旦有人这样做,它就会切换到它并接受.
Mostly all that is done here, is changing Thread.sleep(), for a condition that actually will only move forward on the code as soon a alert() is present in the page. As soon as someone does, it wil switch to it and accept.
您可以找到整个ExpectedConditions类的Javadoc 此处.
You can find the Javadoc for the whole ExpectedConditions class here.
更多推荐
使用Selenium WebDriver和Google Chrome不能关闭警报.
发布评论