Java Explict wait无法持续工作,无法给出错误消息,其他元素正在接收单击

Java Explict wait无法持续工作,无法给出错误消息,其他元素正在接收单击,java,selenium,Java,Selenium,我使用的是页面对象模型 我的Firefox版本是47.01,Selenium服务器版本是2.53.1 执行testNG时出现错误: public void waitForElement(Wait<WebDriver> wait){ System.out.println("#####Insidewait"); if(wait == null){ //this.wait = new WebDriverWait(myBrowser, 20);

我使用的是页面对象模型

我的Firefox版本是47.01,Selenium服务器版本是2.53.1

执行testNG时出现错误:

public void waitForElement(Wait<WebDriver> wait){

    System.out.println("#####Insidewait");

    if(wait == null){
    //this.wait = new WebDriverWait(myBrowser, 20);

        this.wait = new FluentWait<WebDriver>(myBrowser)
                //Timeout time is set to 20
                .withTimeout(20, TimeUnit.SECONDS)
                // polling interval
                .pollingEvery(500, TimeUnit.MILLISECONDS)
                //ignore the exception
                .ignoring(NoSuchElementException.class, ElementNotVisibleException.class);
    }
}
By upgrade = By.xpath("//a[text()='Upgrade']");
By loader = By.xpath("//div[@id='dashBoardLoader'and contains(@style,'display: block')]");
By logout = By.xpath("//a[text() = 'Logout']");
Home home = null;
LogIN login = null;@Test
public void startTest() throws Exception {

    login.loginToApplication(repository.getProperty("username"), repository.getProperty("password"));
    wait.until(ExpectedConditions.invisibilityOfElementLocated(loader));
    wait.until(ExpectedConditions.visibilityOfElementLocated(upgrade)).isDisplayed();
    WebElement btnUpgrade = myBrowser.findElement(upgrade);
    if(btnUpgrade.isDisplayed()){
        //myBrowser.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        home.ClickOnLogout();
        wait.until(ExpectedConditions.urlContains(logoutURL));
        System.out.println("Clicked on logout successfully..");
    }
    else
        System.out.println("Unable to click on logout.");
}
失败:startTest org.openqa.selenium.WebDriverException:元素为 在点(1258,50)处不可单击。其他元素将收到 单击:命令持续时间或超时:79 毫秒

以下是我已经尝试过的东西:

public void waitForElement(Wait<WebDriver> wait){

    System.out.println("#####Insidewait");

    if(wait == null){
    //this.wait = new WebDriverWait(myBrowser, 20);

        this.wait = new FluentWait<WebDriver>(myBrowser)
                //Timeout time is set to 20
                .withTimeout(20, TimeUnit.SECONDS)
                // polling interval
                .pollingEvery(500, TimeUnit.MILLISECONDS)
                //ignore the exception
                .ignoring(NoSuchElementException.class, ElementNotVisibleException.class);
    }
}
By upgrade = By.xpath("//a[text()='Upgrade']");
By loader = By.xpath("//div[@id='dashBoardLoader'and contains(@style,'display: block')]");
By logout = By.xpath("//a[text() = 'Logout']");
Home home = null;
LogIN login = null;@Test
public void startTest() throws Exception {

    login.loginToApplication(repository.getProperty("username"), repository.getProperty("password"));
    wait.until(ExpectedConditions.invisibilityOfElementLocated(loader));
    wait.until(ExpectedConditions.visibilityOfElementLocated(upgrade)).isDisplayed();
    WebElement btnUpgrade = myBrowser.findElement(upgrade);
    if(btnUpgrade.isDisplayed()){
        //myBrowser.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        home.ClickOnLogout();
        wait.until(ExpectedConditions.urlContains(logoutURL));
        System.out.println("Clicked on logout successfully..");
    }
    else
        System.out.println("Unable to click on logout.");
}
  • 正在等待给出错误的元素。 结果:等待仍然失败
  • 设置断点并进行调试。 结果:相同的代码在调试模式下绝对可以正常工作,没有错误
  • 我试过了,不要等。 结果:再次执行失败,但在调试模式下工作正常 我在Test_基类中的等待代码,其中声明了等待:

    public void waitForElement(Wait<WebDriver> wait){
    
        System.out.println("#####Insidewait");
    
        if(wait == null){
        //this.wait = new WebDriverWait(myBrowser, 20);
    
            this.wait = new FluentWait<WebDriver>(myBrowser)
                    //Timeout time is set to 20
                    .withTimeout(20, TimeUnit.SECONDS)
                    // polling interval
                    .pollingEvery(500, TimeUnit.MILLISECONDS)
                    //ignore the exception
                    .ignoring(NoSuchElementException.class, ElementNotVisibleException.class);
        }
    }
    
    By upgrade = By.xpath("//a[text()='Upgrade']");
    By loader = By.xpath("//div[@id='dashBoardLoader'and contains(@style,'display: block')]");
    By logout = By.xpath("//a[text() = 'Logout']");
    Home home = null;
    LogIN login = null;@Test
    public void startTest() throws Exception {
    
        login.loginToApplication(repository.getProperty("username"), repository.getProperty("password"));
        wait.until(ExpectedConditions.invisibilityOfElementLocated(loader));
        wait.until(ExpectedConditions.visibilityOfElementLocated(upgrade)).isDisplayed();
        WebElement btnUpgrade = myBrowser.findElement(upgrade);
        if(btnUpgrade.isDisplayed()){
            //myBrowser.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            home.ClickOnLogout();
            wait.until(ExpectedConditions.urlContains(logoutURL));
            System.out.println("Clicked on logout successfully..");
        }
        else
            System.out.println("Unable to click on logout.");
    }
    

    这可能是时间问题。期望
    ExpectedConditions.visibilityOfElementLocated
    并不真正等待可视性。它等待元素以正大小和不透明度显示,但该元素仍可能被另一个元素覆盖

    要解决此问题,您可以重试单击,直到成功:

    new WebDriverWait(driver, 6).until((WebDriver drv) -> {
        try {
            element.click();
            return true;
        } catch (WebDriverException ex) {
            return false;
        }
    });
    
    如果元素显示时带有一些过渡效果,也可以尝试等待特定的大小:

    // wait minimum width / height
    new WebDriverWait(driver, 6).until((WebDriver drv) -> {
        Dimension size = element.getSize();
        return size.getWidth() > 15 && size.getHeight() > 15 ? element : null;
    }).click();
    

    我已经遇到过很多次了。通常是在关闭一个对话框后,对话框的模式(灰色)背景关闭得不够快,等等。我所做的是将一对等待链接在一起,我等待模式背景(或其他)不可见,然后等待元素可单击/可见

    错误消息应该告诉您哪个元素将接收单击。。。这就是你想等着看不见的人

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.invisibilityOfElementLocated(locator)); // blocking element
    wait.until(ExpectedConditions.elementToBeClickable(locator)).click(); // desired element
    

    好的,非常感谢你的帮助

    我的应用程序在DOM上从服务器渲染控件,但元素的可见性在客户端使用CSS属性(如display:block和display:none)进行控制

    我使用的函数用于检查DOM(服务器呈现)中是否存在元素,因此wait返回true,而同步失败;而我却认为等待失败了

    通过使用客户端属性的函数解决了等待时间问题

    ExpectedConditions.attributeToBe(定位器、属性、值)

    i、 e而不是

    等待.直到(预期条件.不可见性FelementLocated(元素))

    wait.until(ExpectedConditions.visibilityOfElementLocated(element)).isDisplayed()

    等待.until(ExpectedConditions.presenceofElement(element)).isDisplayed()

    已使用


    ExpectedConditions.attributeToBe(定位器、属性、值)

    既然等待对我来说不起作用,那么有没有新的方法来做到这一点,或者有没有办法让等待对我起作用。。i、 实际上,请等待几秒钟。您是否尝试过使用ExepctedConditions.elementToBeClickable(…..)@Grasshopper,是的..事实1。预期条件。不可见性FelementLocated(以前的元素)2。预期条件。元素(..)的存在3。ExpectedCondition.ElementToBelickable(..)在调试模式下执行此操作的方式与实时执行此操作的方式有什么区别?你会一步一步地复习吗?这可能是时间问题,你是对的。。早期与ExpectedConditions结合使用的函数根据服务器的呈现返回状态,因此wait立即从DOM状态返回true;而不是等待某个时刻,然后返回真实。我使用了使用attributesToBe函数的变通方法;它在加载DOM后检查HTML标记属性的更改。。工作!