我是selenium的新手,想使用明确等待 chrome驱动程序。但我无法使用ExpectedConditions类,因为它说“org.openqa.selenium.support.ui .ExpectedConditions“无法解析。我也没有在 WebDriverWait 类中获取直到()方法。我共享了我正在使用的代码和pom文件。请指导我出错的地方。
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>
答案 0 :(得分:1)
您看到的错误说明了一切:
sessionStorage.setItem('labe', JSON.stringify(labe))
从您发布的代码中可以清楚地看出,您错过了以下导入:
"org.openqa.selenium.support.ui.ExpectedConditions" can not be resolved
最后,您已尝试将 ExpectedConditions 作为:
import org.openqa.selenium.support.ui.ExpectedConditions;
如果您将ExpectedConditions
的until(ExpectedConditions.visibilityOfElementLocated(driver.findElement(By.xpath(lookup))));
视为visibilityOfElementLocated
,则该方法会明确接受参数By locator
,因为您已经通过了WebElement element
如下:
JavaDocs
解决方案将是:
添加必需的导入:
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
答案 1 :(得分:0)
将以下依赖项添加到您的 pom.xml
<dependency>
<groupId>info.cukes</groupId>
<artifactId>selenium-support</artifactId>
<version>2.52.0</version>
</dependency>
然后转到您的项目-->Maven-->更新