有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

我可以在IE8中成功地做到这一点。但无法在Firefox中验证相同的功能。我尝试过不同的等待 1.首被告。管理()。超时()。隐式等待(60,时间单位秒)

  1. WebDriverWait wait=新的WebDriverWait(驱动程序,10); 等待withTimeout(30,时间单位为秒); 等待直到(预期条件。被定位元素的可视性)(通过.id(“ElmId”))

  2. 等待等待=新建FluentWait等待=新建FluentWait(d1)。带超时(60秒); 等待直到(新函数() 等待直到(预期条件、可视性(d1.findElement(By.id)(“elementid”))))

有没有人面临过类似的问题?有解决办法吗


共 (2) 个答案

  1. # 1 楼答案

    点击一个按钮后会出现这个“成功消息”:它是用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

  2. # 2 楼答案

    也许你可以尝试其他类型的条件?或者您也可以通过重写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;
            }
        });
    

    像这样的事情好几次都很有用