无法解决 ExpectedConditions

ExpectedConditions can not be resolved

我是 selenium 的新手,想使用 chrome 驱动程序 的显式等待。但我无法使用 ExpectedConditions class 正如它所说"org.openqa.selenium.support.ui.ExpectedConditions" 不能 resolved.Also 我没有在 WebDriverWait[= 中获取 until() 方法29=] class.Have 分享了我的代码和 pom 文件 using.Please 指导我哪里出错了。

Selenium 脚本

package seleniumutils;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;//Error at this line 


public class SeleniumUtils {

    WebDriver driver;
    final String webUrl = "url";
    final String pathToChromeDriver = "path to chromedriver.exe";
    final String KEY_WEB_DRIVER = "webdriver.chrome.driver";
    WebDriverWait wait;

    public void initDriver() {
        System.setProperty(KEY_WEB_DRIVER, pathToChromeDriver);
        driver = new ChromeDriver();
        driver.get(webUrl);
        //         driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        String str = driver.getCurrentUrl();
        System.out.println("The current URL is " + str);
        wait = new WebDriverWait(driver, 10);
    }


    public List < WebElement > getElementsByID(String id) {
        List < WebElement > list = (List < WebElement > ) driver.findElement(ById.id(id));
        return list;
    }

    public void waitForScanner(String lookup) {

        WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(driver.findElement(By.xpath(lookup))));
    }

POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.interact-ez</groupId>
    <artifactId>interact-ez</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>interact-ez</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>5.0.4</version>
        </dependency>
    </dependencies>
</project>

屏幕截图

您看到的错误说明了一切:

"org.openqa.selenium.support.ui.ExpectedConditions" can not be resolved

从您发布的代码中可以清楚地看出您错过了包含以下 导入

import org.openqa.selenium.support.ui.ExpectedConditions;

最后,您尝试了 ExpectedConditions 作为 :

until(ExpectedConditions.visibilityOfElementLocated(driver.findElement(By.xpath(lookup))));

如果您查看 ExpectedConditions as visibilityOfElementLocated the method clearly accepts the parameter By locator where as you have passed WebElement elementJavaDocs,如下所示:

visibilityOfElementLocated(By locator)
An expectation for checking that an element is present on the DOM of a page and visible.

解决方案

解决方案将是:

  • 添加需要的import :

    import org.openqa.selenium.support.ui.ExpectedConditions;
    
  • 将对 visibilityOfElementLocated 的调用更改为:

    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(lookup)));
    

将以下依赖项添加到您的 pom.xml

  <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>selenium-support</artifactId>
        <version>2.52.0</version>
    </dependency>

然后转到您的项目-->Maven-->更新