Webdriver Automation - Unable to find element using xpath Webdriver Automation - Unable to find element using xpath selenium selenium

Webdriver Automation - Unable to find element using xpath


Try using CSS

WebElement element = new WebDriverWait(driver,30).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[id*='DERIVED_REGFRM1_LINK_ADD_ENRL'][class='SSSBUTTON_CONFIRMLINK']")));


It's possible the page object has not loaded at the time Webdriver is searching for it. Try to locate the step that is failing, and place an implicit wait/sleep before it.

If Webdriver is able to find the page object after you know it's a page load issue.

  1. Identify the step the test is failing on.
  2. Place a Wait/Sleep before the step ie... 20 seconds (way more than enough).
  3. Execute your test again.
  4. If the test now passes, Webdriver searched for the page object before it fully loaded.


I think it can be because element you are trying to find is not present on the page at the moment driver searches for it. One of solution would be to define pageLoadTimeout. You can do that with such line of code:

driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS); 

You should increase the time if your page is loading longer.Second thing that I am using to ensure driver waits while page is loaded (especially when content is dynamic loaded by some scripts) is custom waitForPageLoaded function, which should be called after your webdriver opens specific page or clicks on button/link that does the page open action.

Example of the function:

public void waitForPageLoaded(WebDriver driver) { ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {    public Boolean apply(WebDriver driver) {      return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");    }  }; Wait<WebDriver> wait = new WebDriverWait(driver,30);  try {          wait.until(expectation);  } catch(Throwable error) {          assertFalse("Timeout waiting for Page Load Request to complete.",true);  }}