1

I have a piece of code which gets a web element and checks if the element is displayed or not.

Code:

WebElement element = getDriver().findElement(By.linkText("Expand"));
 if(element.isDisplayed()){
                printInfo("Expand is displayed");
            }
  else{
   printInfo("Expand is NOT displayed");
  }

This same piece of code works fine on my local machine on the same environment (prints "Expand is displayed"). However, on the remote machine (using Jenkins), with the same code on the same environment, it fails and executes the else block (prints "Expand is NOT displayed").

I can visually see the element being displayed on the application during execution and there is no issue of synchronization either.

Why is the same piece of code behaving differently on a different machine when it is the same environment?

OS on both the machines: MacOS.

Browser on both the machines: Firefox.

Guy
  • 46,488
  • 10
  • 44
  • 88
AutoTester999
  • 528
  • 1
  • 6
  • 25

1 Answers1

1

isDisplayed() doesn't garuntees if the element is visible within the DOM Tree. Instead you need to induce WebDriverWait for the visibilityOfElementLocated() within a try-catch{} block and you can use the following Locator Strategy:

try {
  new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Expand")));
  printInfo("Expand is visible");
}
catch(TimeoutException e) {
  printInfo("Expand is NOT visible");
}

Reference

You can find a relevant detailed discussion on different ExpectedConditions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352