使用Selenium WebDriver和Google Chrome不能关闭警报. [英] Alert doesn't close using Selenium WebDriver with Google Chrome.

查看:263
本文介绍了使用Selenium WebDriver和Google Chrome不能关闭警报.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Selenium脚本可在rediff.com上打开警报:

I have the following Selenium script for opening alert on rediff.com:

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("http://www.rediff.com/");
        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("http://www.rediff.com/");
        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不能关闭警报.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆