Able to find xpath, but trying to click button throws exception



  • I tried to click on an element using the code below but it is throwing Exception: No Such Element Selections

    WebElement clear=driver.findElement(By.xpath(PageUtility.OPEN_LOCATION_POPUP_XPATH));        
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", clear);
    

    and while finding xpath it gave the 1\1 matching.



  • If you're 100% sure that the XPath selector is correct the problem can have at least 2 possible causes:

    1. Element hasn't been fully loaded yet, it might appear later than document.readyState reports complete as it might be added later as the result of an AJAX call execution. If this is the case - consider using an Explicit Wait to ensure that the element is there prior to attempting interacting with it.

      WebDriverWait wait = new WebDriverWait(driver, 10);
      wait.until(ExpectedConditions.elementToBeClickable(By.xpath(PageUtility.OPEN_LOCATION_POPUP_XPATH)));
      
    2. The element might reside in an iframe, if this is the case you will need to switch to the relevant context


    The element can be hidden in the Shadow DOM, if it's so - you won't be able to locate it using findElement() function and will need to consider another strategy of element location.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2