Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wait.until(ExpectedConditions) doesnt work any more in selenium

Tags:

selenium

So far I used 2.45.0 version of selenium and all my waits were done in this way:

WebDriverWait wait = new WebDriverWait(webKitUtility.getWebDriver(), 5); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("role"))); 

But I updated selenium to 3.1.0 and I am getting the error:

"The method until(Predicate) in the type FluentWait is not applicable for the arguments (ExpectedCondition)"

I see that from 2.45.0 to 3.1.0 some things are deprecated. I am trying to investigate what is the best way to do it now, but I am not sure. Most of the things I'm finding on google are old information explaining the same way I was using so far.

like image 723
lijep dam Avatar asked Feb 23 '17 16:02

lijep dam


People also ask

What is Expectedconditions in explicit wait?

Rather than waiting for a specified time duration (also called Implicit Wait), wait is performed on a certain condition. These are called Expected Conditions in Selenium. An explicit wait (for a maximum time duration) can be performed till a 'certain condition' (e.g. till the Element is not visible) is met.

Is implicit wait deprecated in Selenium?

Method SummaryDeprecated. Use implicitlyWait(Duration) Specifies the amount of time the driver should wait when searching for an element if it is not immediately present. Specifies the amount of time the driver should wait when searching for an element if it is not immediately present. Deprecated.

Which wait command will wait indefinitely for page load in Selenium?

PageLoadTimeout Command This command establishes the time WebDriver must wait for a page to completely load before triggering an error. In case the timeout set is negative, the page load time can be indefinite.

How do you use wait until element is visible in Selenium?

Selenium: Waiting Until the Element Is Visiblevar wait = new WebDriverWait(driver, TimeSpan. FromSeconds(20)); As you can see, we give the WebDriverWait object two parameters: the driver itself and a TimeSpan object that represents the timeout for trying to locate the element.


2 Answers

I had the same issue.

I fixed it by using the not deprecated .until() method of WebDriverWait and by adding the following to my maven pom.xml:

<dependency>       <groupId>com.google.guava</groupId>       <artifactId>guava</artifactId>       <version>21.0</version> </dependency> 

Other than that, my code looks exactly like before.

To be more specific there are now two .until() methods.

The old one (which is deprecated):
public void until(final Predicate<T> isTrue) {}

And the new one:
public <V> V until(Function<? super T, V> isTrue) {}

like image 157
Robert G Avatar answered Sep 30 '22 20:09

Robert G


Note if you are using Maven that order of the dependencies do matter.

For example:

public static void main(String[] args) {     System.setProperty("webdriver.gecko.driver", "/Users/me/geckodriver");     final WebDriver driver = new FirefoxDriver();     driver.get("https://www.google.com");     final WebDriverWait wait = new WebDriverWait(driver, 5);     final By feelLuckyXpath = By.xpath("//div[@class='jsb']/center/input[@type='submit' and @name='btnI']");     wait.until(ExpectedConditions.visibilityOfElementLocated(feelLuckyXpath)).click();     driver.close(); } 

this code works fine with the following maven dependencies:

<dependency>     <groupId>org.seleniumhq.selenium</groupId>     <artifactId>selenium-java</artifactId>     <version>3.8.1</version> </dependency>  <dependency>     <groupId>com.google.api-client</groupId>     <artifactId>google-api-client</artifactId>     <version>1.22.0</version> </dependency> 

but it may fail with reordered one:

<dependency>     <groupId>com.google.api-client</groupId>     <artifactId>google-api-client</artifactId>     <version>1.22.0</version> </dependency>  <dependency>     <groupId>org.seleniumhq.selenium</groupId>     <artifactId>selenium-java</artifactId>     <version>3.8.1</version> </dependency> 

In this case because the google-api-client contains:

<groupId>com.google.guava</groupId> <artifactId>guava-jdk5</artifactId> 

as dependency which shadows the guava lib in the selenium lib.

In this case the error is:

no instance(s) of type variable(s) V exist so that ExpectedCondition<> ...

method until in class org.openqa.selenium.support.ui.FluentWait cannot be applied to given types; required: java.util.function.Function found: org.openqa.selenium.support.ui.ExpectedCondition reason: cannot infer type-variable(s) V (argument mismatch; org.openqa.selenium.support.ui.ExpectedCondition cannot be converted to java.util.function.Function)

like image 36
nyxz Avatar answered Sep 30 '22 19:09

nyxz