Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait until Page Contains Element works but not Element Should Contain

Trying to verify if an element contains a specific text, but the keyword Element Should Contain does not find the text even if I explicitly put in the xpath attribute that it contains the text. However the keyword Wait Until Page Contains Element works.

Ran the script below and got the following results

#this was successful
Wait until Page Contains Element  xpath://div[@id="header"]//span[contains(text(), "text")]  

#this resulted to: should have contained text 'text' but its text was ''
Element Should Contain  xpath://div[@id="header"]//span[contains(text(), "text")]  text  
like image 387
E.AS.P. Avatar asked Dec 29 '25 11:12

E.AS.P.


1 Answers

There is a difference between the Element Should Contain and Wait until Page Contains Element.

The first will only check if at the moment the DOM contains the element with a substring of that text, while the last one will wait (by default) up to 5 seconds for that element to appear in DOM in order to validate if it exists. Or, to rephrase: the first one checks if the condition is correct, the other one listens for changes in the DOM to detect the needed value. Maybe your text is shown after some loading time?

From the documentation:

Element Should Contain:

Verifies that element locator contains text expected. ... Use Element Text Should Be if you want to match the exact text, not a substring.

Wait Until Page Contains Element:

Waits until element locator appears on current page. Fails if timeout expires before the element appears.

like image 177
Nomce Avatar answered Jan 03 '26 17:01

Nomce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!