What are some reasons Selenium wouldn't find an Element?

Question:

I’m writing an automated test for a Web-based application that’s coded in JavaScript and uses the Dojo framework. I can find the elements I want the bot to find in Developer Tools using well-defined XPATHS, but the automated test fails to find the text the XPATH leads to.

My test code looks something like this:

if verified:
     verify_detail.set_pass()
else:
     raise AssertionError("Cannot verify detail. Text did not match expected value.")

And the text I’m looking for on the UI isn’t misspelled in the config file.

Anyone else have this problem? XPATH works in Developer Tools but fails when the test is run.

Edit 1:

I think I’ve found a clue as to what’s causing this problem. The bot has to navigate to a panel called the details panel. The details panel, when opened, sits on top of another panel called the movie panel. Imagine a list of movie titles and the details panel is opened by right clicking the movie and selecting ‘Details’ from a dropdown.
So I’ve basically got a stack of panels, and my XPATH queries are hitting the pane beneath the details pane, which is where I really need to to look.

Has anyone else ever had this problem?

Asked By: Trevor

||

Answers:

There is not much information to go on, but I realise that it is very hard to deliver a complete example for such cases.

The most common reason for this kind of behaviour is that the element in question is not present immediately on the page, but rather some JavaScript processing has to happen before it will appear.

The most common solution is using "implicit waits", see for example:
https://selenium-python.readthedocs.io/waits.html#implicit-waits

Be aware that this might affect performance of tests in other places, specifically when checking for absence of elements, if it is switched on constantly.

The obvious alternative is explicit waiting.

Another useful tool for analysing situations like this is setting debugger breakpoints in the browser on DOM elements that you are interested in, to see how the element is changing while the page is loading.

Answered By: Thomas Hirsch