Selenium 偶尔引起错误的测试

Selenium 偶尔引起错误的测试,selenium,webdriver,selenium-webdriver,Selenium,Webdriver,Selenium Webdriver,我在运行SeleniumWebDriver测试时遇到了一个非常奇怪的问题 我的代码 driver.findElement(By.id("id")).click(); driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS); driver.findElement(By.xpath("//a[starts-with(@href,'/problematic_url')]")).click(); driver.manage().t

我在运行SeleniumWebDriver测试时遇到了一个非常奇怪的问题

我的代码

driver.findElement(By.id("id")).click();
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[starts-with(@href,'/problematic_url')]")).click();
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);
driver.findElement(By.className("green_true")).click();
这些元素实际上是存在的。我甚至可以看到webdriver点击了有问题的url,但是什么也没发生。浏览器无法进入页面,也找不到green_true元素。导致错误。但只是偶尔。有时测试会按它应该的方式运行

谁能告诉我这是怎么回事


我不能使用精确的URL,因为它们随所选语言的不同而不同。

好吧。建议按以下方式进行修改: 而不是

driver.findElement(By.id("id")).click();
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[starts-with(@href,'/problematic_url')]")).click();
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);
driver.findElement(By.className("green_true")).click();
请尝试使用以下内容:

public WebElement fluentWait(final By locator){
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                .withTimeout(30, TimeUnit.SECONDS)
                .pollingEvery(5, TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class);

        WebElement foo = wait.until(
new Function<WebDriver, WebElement>() {
            public WebElement apply(WebDriver driver) {
                        return driver.findElement(locator);
                }
                }
);
                           return  foo;              }     ;

fluentWait(By.id("id")).click();
fluentWait(By.xpath("//a[starts-with(@href,'/problematic_url')]")).click();
fluentWait(By.className("green_true")).click();
publicwebelement fluentWait(最终由定位器确定){
等待等待=新建FluentWait(驱动程序)
.带超时(30,时间单位。秒)
.轮询间隔(5,时间单位。秒)
.忽略(NoSuchElementException.class);
WebElement foo=wait.until(
新函数(){
公共WebElement应用(WebDriver){
返回驱动程序findElement(定位器);
}
}
);
返回foo;};
fluentWait(By.id(“id”))。单击();
fluentWait(By.xpath(“//a[以(@href,/problem_url'))开头)。单击();
fluentWait(By.className(“green_true”))。单击();
问题可能是,在与元素交互(单击等)后,页面上可能会出现一些AJAX。我想我们需要使用一种稍微健壮一点的等待机制

一条建议:当您获得webelement或css选择器的xpath时,不要忘记验证在fireBug、ffox扩展中找到的定位器。
注意。

单击动态元素时,请尝试使用显式等待。等待元素出现在web浏览器上或对其应用了操作。您可以使用以下模式:

final FluentWait<WebDriver> wait =
            new FluentWait<WebDriver>(getDriver())
                    .withTimeout(MASK_PRESENCE_TIMEOUT, TimeUnit.SECONDS)
                    .pollingEvery(100, TimeUnit.MILLISECONDS)
                    .ignoring(NoSuchElementException.class)
                    .ignoring(StaleElementReferenceException.class)
                    .withMessage("Time out while waiting the element is loaded");

    wait.until(new Predicate<WebDriver>() {

        @Override
        public boolean apply(final WebDriver driver) {
            return ! driver.findElements(By.id("id")).isEmpty(); 
        }

    });
final FluentWait等待=
新建FluentWait(getDriver())
.withTimeout(掩码\存在\超时,时间单位为秒)
.pollingEvery(100,时间单位为毫秒)
.忽略(NoSuchElementException.class)
.忽略(StaleElementReferenceException.class)
.withMessage(“等待加载元素时超时”);
wait.until(新谓词(){
@凌驾
公共布尔应用(最终WebDriver){
return!driver.findElements(By.id(“id”)).isEmpty();
}
});

同样的问题仍然存在。由于测试通常在第一次运行时通过,这可能与驱动程序初始化有关吗?您能提供您得到的异常的来源吗?我得到的错误无法定位元素:{“方法”:“类名”,“选择器”:“green_true”}请仔细验证您是否正确找到WebElement。-这是一本很好的xpath、css、DOM手册。对我来说,这可能是问题之一:定位器不正确,或者selenium正试图与页面上仍然不存在的元素交互,原因如下:服务器响应时间长。在我看来,如果所有定位器都是正确的,并且firebug批准了它,那么其他可能的问题应该由fluent wait处理