如何编写明确的等待,直到找到特定的WebElement

时间:2019-02-01 15:38:10

标签: java selenium-webdriver

这是显式等待的示例代码:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath-here")));

我想将WebElement作为方法中的参数传递,并等待找到该WebElement:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(MyWebElement));

我不确定是否已经存在这样的选项,并且可以通过其他方式完成此操作,因为在我传递WebElement代替By.xpath("")的情况下,我遇到了异常,这不是正确的方法。

2 个答案:

答案 0 :(得分:3)

您需要使用visibilityOf预期条件。

代码如下:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(MyWebElement));

希望对您有帮助!

答案 1 :(得分:0)

是的,您不能通过Web元素代替XPath,因为方法'visibilityOfElementLocated()'将仅接受按定位符。

假设您的方法如下:

public static void waitUntilLocated(WebDriver driver, int waitingTime, By locator) {
    new WebDriverWait(driver, waitingTime).until(ExpectedConditions.visibilityOfElementLocated(locator));
}

然后,您可以按以下方式存储“按”定位符:

By someXPath = By.xpath("some xpath here");

然后您可以将“按定位符”作为参数传递给方法

waitUntilLocated(driver, 30, someXPath);

下面是整个代码:

import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Test1 {

    public static void waitUntilLocated(WebDriver driver, int waitingTime, By locator) {
        new WebDriverWait(driver, waitingTime).until(ExpectedConditions.visibilityOfElementLocated(locator));
    }

    public static void main(String ...ali) throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setBrowserName("chrome");
        WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:1234/wd/hub"), capabilities);
        driver.get("http://www.google.com");
        driver.findElement(By.name("q")).sendKeys("Alicse3"+Keys.ENTER);

        // Checking element is located or not?
        By someXPath = By.xpath("some xpath here");
        waitUntilLocated(driver, 30, someXPath);

        // Checking some other element
        By someId = By.id("some id");
        waitUntilLocated(driver, 30, someId);
    }
}

或者如果您想将Web元素传递给该方法并且不想使用'visibilityOfElementLocated()',也可以执行以下操作:

import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Test1 {

    public static void waitUntilLocated(WebDriver driver, int waitingTime, WebElement element) {
        new WebDriverWait(driver, waitingTime).until(ExpectedConditions.visibilityOf(element));
    }

    public static void main(String ...ali) throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setBrowserName("chrome");
        WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:1234/wd/hub"), capabilities);
        driver.get("http://www.google.com");
        driver.findElement(By.name("q")).sendKeys("Alicse3"+Keys.ENTER);

        // Checking element is located or not?
        WebElement element = driver.findElement(By.xpath("Some XPath"));
        waitUntilLocated(driver, 30, element);

        // Checking some other element
        element = driver.findElement(By.id("Some ID"));
        waitUntilLocated(driver, 30, element);
    }
}

希望对您有帮助...