等待在 Firefox 浏览器中不起作用(Webdriver selenium 2.0 +Java)

Posted

技术标签:

【中文标题】等待在 Firefox 浏览器中不起作用(Webdriver selenium 2.0 +Java)【英文标题】:Wait not working in Firefox browser (Webdriver selenium 2.0 +Java) 【发布时间】:2012-05-17 16:11:45 【问题描述】:

我正在使用 WebDriver(Eclipse -Java) 来自动化注册页面。单击“注册”按钮时,会显示“成功消息”,需要对其进行验证。

我可以在 IE8 中成功地做到这一点。但无法在 Firefox 中验证相同的内容。我尝试过不同的等待 1. d1.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

    WebDriverWait 等待 = 新 WebDriverWait(驱动程序, 10); wait.withTimeout(30, TimeUnit.SECONDS); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ElmId"));

    等待等待 = 新 FluentWait 等待 = 新 FluentWait(d1).withTimeout(60, SECONDS); 等待。直到(新功能() wait.until(ExpectedConditions.visibilityOf(d1.findElement(By.id("elementid"))));

有没有人遇到过类似的问题?有什么解决办法吗?

【问题讨论】:

【参考方案1】:

也许您可以尝试其他条件类型?或者您也可以尝试通过重写 apply 方法来编写自己的。当使用提供的条件还不够时,我很少遇到这种情况。只有在使用我自己版本的 apply 方法后才成功。

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeoutInSeconds, TimeUnit.SECONDS)
        .pollingEvery(pollingInterval,
            TimeUnit.MILLISECONDS);
    return wait.until(new ExpectedCondition<WebElement>() 

        @Override
        public WebElement apply(WebDriver arg0) 
            List<WebElement> findElements = driver.findElements(By.className(someClassName));
            for (WebElement webElement : findElements) 
                if (webElement.getText().equals(string)) 
                    return webElement;
                
            
            return null;
        
    );

e。 G。几次这样的事情很有帮助。

【讨论】:

【参考方案2】:

单击按钮后的“成功消息”:是用 ajax/javascript 显示还是重新加载页面?

如果您使用 js 执行此操作,有时可能无法使用 WebDriver 命令验证消息,您也需要使用 js 进行验证。比如:

Object successMessage = null;
int counter = 0;

    while ((successMessage == null) && counter < 5)
    
        try
        
            ((JavascriptExecutor)driver).executeScript("return document.getElementById('yourId')");
        
        catch (Exception e)
        
            counter +=1;
        
    

    if (successMessage != null) //would be better to use some assertion instead of conditional statement
    
        //OK
    
    else
    
        //throw exception
    

while 循环是伪等待功能的丑陋方式。如果您不需要等待元素,您也可以将其删除。

替代方案可能是

Object result = ((JavascriptExecutor)driver).executeScript("return document.body.innerhtml"); 
String html = result.toString();

然后手动解析html。

【讨论】:

以上是关于等待在 Firefox 浏览器中不起作用(Webdriver selenium 2.0 +Java)的主要内容,如果未能解决你的问题,请参考以下文章

卢比符号在Firefox中不起作用

为啥 HTML5 拖放功能在 Firefox 中不起作用?

Angular 1.3 动画在 Firefox 中不起作用

SVG 圆的样式在 Firefox 中不起作用,浏览器删除了半径属性

自定义 CSS webkit 滚动条在 Firefox 浏览器中不起作用

CORS 同步请求在 Firefox 中不起作用