How to Use Selenium to Find Element

Reading Time: 2 minutes

This week Alex looks at Selenium Find Element and a checklist of tips of things to do when an element cannot be found.

 

What to do when an web element cannot be found? We all had to deal with situations when a test script cannot find an element using code as follows:

WebElement element = driver.findElement(locator);

If findElement() cannot find the element, a NoSuchElementException is generated.

The element is in the page. The locator of the element is correct as well. If findElement() does not work, what else can you do?

 

1. Use a Different Locator

If you used the element’s id as locator, try a CSS locator instead.

Or an XPATH locator.

2. Synchronize the code with the page

Explicit waits and expected conditions are great when the page loads slowly.

They will keep trying to find the element until the element is available and it has a specific state:

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(locator));

Make sure that the expected condition matches the element’s status:

presenceOfElementLocated() – if the element is not visible

visibilityOfElementLocated() – if the element is visible

elementToBeClickable() – if the element is clickable

3. Is the element included in a frame?

If the element is in a frame, you need to switch the current driver’s context to the frame before trying to find it:

driver.switchTo().frame("header");
WebElement element = driver.findElement(locator);
driver.switchTo().defaultContent();

Remember to switch back from the frame to the main page when you are done interacting with frame elements.

4. Use Javascript

If none of the previous tips works, you can try finding the element by Javascript:

String jsQuery = 
"function getElement()" +
"{var element = document.getElementById('id123'); return element; }; " +
"return getElement()";

WebElement element = (WebElement) jsExecutor.executeScript(jsQuery);

 

5. Reload the page

Sometimes, the element is just in an invalid state and nothing works.

You can reload the page and retry.

This worked for me in a number of occasions.

Have other tips for Selenium Find element?

Please share them in the comments below

 

About The Author

Alex SiminiucAlex lives in Vancouver, Canada. He has worked as a software tester since 2005. Alex teaches manual testers test automation with Selenium WebDriver and Java. Alex blogs on testing and automation at http://test-able.blogspot.ca.

About the Author

Ronan

Hi everyone. I'm part of the team here at TEST Huddle. I'm doing my best to make sure everyone gets the best out of the TEST Huddle community. So if you have any thoughts about the community, get in contact with me.
Find out more about @ronan

One Response to “How to Use Selenium to Find Element”

  1. * We only use polling mechanisms with explicit timeouts. So if our tests timeout, we can be sure the element isn’t going to be there.
    * When developing, I try my selectors in the browser directly. It gives much faster feedback. Chrome supports searching selectors in the Elements view. Also, you can use document.querySelectorAll(“…”) to test css locators and $x(“…”) to use xpath locators.
    * We only use CSS, not XPath, simply because its easier to read and most web developers are familiar with it. It’s less powerful than xpath, yes… but if you find yourself needing XPath, it usually indicates the implementation can be improved as well.

Leave a Reply

Upcoming WebinarTurning Uncertainty into a Catalyst for Change