0

I am using Selenium and attempting to locate an element (table cell in a web app) by xpath. However, I am getting a NoSuchElementException when running my test cases.

I am positive that the xpath locator is correct, as I can paste it into my DOM and locate the element that way (it is also unique). I have also confirmed that the element is not in a different frame. Here is what my WebElement declaration looks like:

@FindBy(xpath = "//td[text()='info']//parent::tr//td[2]") WebElement txtInfo;

I can locate the element with this xpath locator in my DOM:

What possible reasons could there be for this error?

  • Could you post HTML snippet? We can`t really give any advice without it. – user34705 Nov 20 '18 at 8:05
0

Note: I'm adding as an answer since I don't have the reputation to add a comment. Will edit the reply with an answer once you provide more details.



Coming to the question, would it be possible to check the same xpath with the Chrome developer tools Xpath search option in the Elements panel?

Also, if you can't share the application URL, it would be helpful if you can share the part of the code you are evaluating the xpath for - if possible, HTML for the table.

0

NoSuchElementException happens generally in 2 scenarios.

  1. Webelement locator is wrong. (which doesn't seems to be the situation in your case)
  2. Your are trying to find the webelement even before it is loaded on the page.(This seems to be your problem)

To solve this you should wait till the webelement gets loaded on to the webpage.

One way to do this to use WebDriverWait Class. Sample snippet is below:

/* Initialize the WebDriverWait, with 30 seconds of wait time. */
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("locator_name")));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.