我是硒的新手,想与chrome驱动程序一起使用Explicit等待。但是我无法使用ExpectedConditions类,因为它说“ org.openqa.selenium.support.ui.ExpectedConditions”无法解决。我也收不到WebDriverWait类中的until()方法。共享了我正在使用的代码和pom文件。请指导我要去哪里。
硒脚本
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文件
<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))));
如果将
JavaDocs
的ExpectedConditions
视为visibilityOfElementLocated
,则该方法显然会接受参数By locator
,其中传递了WebElement element
如下:visibilityOfElementLocated(By locator)
An expectation for checking that an element is present on the DOM of a page and visible.
解
解决方案将是:
添加所需的导入:
import org.openqa.selenium.support.ui.ExpectedConditions;
将呼叫更改为
visibilityOfElementLocated
:WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(lookup)));