代码之家  ›  专栏  ›  技术社区  ›  Shoaib Akhtar

如何编写显式等待,直到找到特定的WebElement

  •  2
  • Shoaib Akhtar  · 技术社区  · 7 年前

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

    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 回复  |  直到 7 年前
        1
  •  3
  •   Ratmir Asanov    7 年前

    你需要使用 visibilityOf 预期条件。

    代码如下:

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

    希望它能帮助你!

        2
  •  0
  •   Ali    7 年前

    是的,不能传递Web元素来代替xpath,因为方法“visibilityOfElementLocated()”只接受定位器。

    假设您的方法如下:

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

    然后您可以像下面这样存储by定位器:

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

    然后可以将“by-locator”作为参数传递给方法

    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);
        }
    }
    

    我希望它有帮助…