Selenium + Java等待元素可见

问题描述 投票:0回答:3

我是Selenium和Java的新手。我想要完成的是等待一个元素出现超时,如果该元素出现在超时运行之前,那么就继续运行。下面的代码将给出一个TimeoutException并停止其余的代码运行。

WebElement myDynamicElement = (new WebDriverWait(driver, 10))
    .until(ExpectedConditions.presenceOfElementLocated(By.id("my_element")));
java selenium
3个回答
0
投票

请尝试这种方式:

public boolean isFind(WebElement my_element) {
   try{
      WebElement myDynamicElement = (new WebDriverWait(driver, 10))
    .until(ExpectedConditions.presenceOfElementLocated(By.id("my_element")));
   }
   catch(TimeoutException exception) {
        return false;
    }
   return true;
}

0
投票

谢谢你的帮助,我花了一些时间来玩它。这是我提出的解决方案。希望它有助于下一个人。请告诉我,如果我搞砸了,请再次感谢。

    try{
        WebDriverWait wait = new WebDriverWait(driver, 5);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@id='signup_email']")));

    }catch (org.openqa.selenium.TimeoutException e){

        JOptionPane.showMessageDialog(null, "Login Complete!");
        return;

    }

-1
投票

你可以使用ExpectedConditions.visibilityOf

WebDriverWait wait = new WebDriverWait(d, 120);
wait.until(ExpectedConditions.visibilityOf(d.findElement(By.xpath("//input[@id='signup_email']"))));
© www.soinside.com 2019 - 2024. All rights reserved.