0

I'm new in selenium Java, can anybody help and give me a simple example how to create a wait.until java command? Please correct my code if wrong thank you

import org.openqa.selenium.support.ui.WebDriverWait;
//lines of code
WebDriverWait waitVar = new WebDriverWait (driver, 1000);
waitVar.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"btnDraftReports\"]"))).click();

or should i use this ?

waitVar.until(ExpectedConditions.elementToBeClickable(By.id("btnDraftReports"))).click();

Thank you and more power

DebanjanB
  • 118,661
  • 30
  • 168
  • 217

4 Answers4

0

Syntactically both the expressions are correct and valid Locator Strategies for Selenium where :

  • By.xpath("//*[@id=\"btnDraftReports\"]") : This expression uses the xpath engine.
  • By.id("btnDraftReports") : This expression uses the id attribute.

However, once you wait for the ExpectedConditions to return the element as you are invoking click() so instead of visibilityOfElementLocated() method you should use elementToBeClickable() method as follows :

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"btnDraftReports\"]"))).click();
//or
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("btnDraftReports"))).click();

Trivia

Now, the debatable question is which locator to use id or xpath?

Since Selenium is in practice id attribute is still considered as the most efficient Locator Strategy.

You will get some more insights in the discussion Official locator strategies for the webdriver

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
0

visibilityOfElementLocated differs from elementToBeClickable behavior wise.

When to use what : All expected conditions depends on scenario.

visibilityOfElementLocated : When you want to wait till the visibility of a web element to be displayed on a web page , then you might wanna use this expected conditions. It's more about visibility of a web element.

Example :

  1. After login into any web site you should let your script know about that login have been successfully done by using visibilityOfElementLocated condition. You might consider your profile picture or any link that appears after login.

elementToBeClickable : When you want to wait till the web element become clickable. It's more about click ability of a web element.

Example :

  1. You must have seen the agreement check box in many web sites while creating an account. The scenario is you can't click on a sign up button until you have checked the read agreement check box. Here you will have to use elementToBeClickable condition.

Rest about the code which I think you have written correctly.

Hope this will help you to understand the basic difference between mentioned conditions.

cruisepandey
  • 6,860
  • 3
  • 9
  • 24
  • Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element ... is not clickable at point (735, 120). Other element would receive the click:
    ...
    Still getting this error message :(
    – ian castillio May 15 '18 at 21:20
  • waitVar.until(ExpectedConditions.elementToBeClickable(By.id(‘btnDraftReports’))); driver.findElement(By.id(“btnDraftReport”)).click(); just make sure element is visible and if there is any scroll down or up , you are performing it well . – cruisepandey May 16 '18 at 02:59
  • Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element ... is not clickable at point (735, 124). Other element would receive the click:
    ...
    error still encountered
    – ian castillio May 16 '18 at 21:15
  • when you are manually doing it , would you have to scroll down to interact with element or any kind of action you have to perform for interaction ? – cruisepandey May 17 '18 at 06:02
0

Try below code

/* This method is used for wait until element found */
public void expliciteWait(WebElement element, int timeToWaitInSec, WebDriver driver) {
    WebDriverWait wait = new WebDriverWait(driver, timeToWaitInSec);
    wait.until(ExpectedConditions.visibilityOf(element));
0

WebElement XYZ= driver.findElement(By.xpath("xpath")); WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.elementToBeClickable(XYZ)); XYZ.click; for better answer you need to show HTML