I am new to selenium and wanted to use Explicit wait with chrome driver.But I am not able to use ExpectedConditions class as it says "org.openqa.selenium.support.ui.ExpectedConditions" can not be resolved.Also I am not getting until() method in WebDriverWait class.Have shared the code and pom file I am using.Please guide where I am going wrong.
Selenium Script
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>
The error you are seeing says it all :
"org.openqa.selenium.support.ui.ExpectedConditions" can not be resolved
It is clear from your published code you have missed out to include the following imports :
import org.openqa.selenium.support.ui.ExpectedConditions;
Finally, you have tried the ExpectedConditions as :
until(ExpectedConditions.visibilityOfElementLocated(driver.findElement(By.xpath(lookup))));
If you look at the JavaDocs
of the ExpectedConditions
as visibilityOfElementLocated
the method clearly accepts the parameter By locator
where as you have passed WebElement element
which is as follows :
visibilityOfElementLocated(By locator)
An expectation for checking that an element is present on the DOM of a page and visible.
The solution will be to :
Add the required import :
import org.openqa.selenium.support.ui.ExpectedConditions;
Change the call to visibilityOfElementLocated
as :
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(lookup)));