0

I have the below element in the page I am currently testing:

<li>
"At least 4 of the following rules must be satisfied:"
<br>
"Password should contain at least 1 upper latin characters (A-Z)"
<br>
"Password should contain at least 1 lower latin characters (a-z)"
"<br>
Password should contain at least 1 digits (0-9)"
<br>
"Password should contain at least 1 special characters: ! @ # $ % ^ & * ( ) _ + [ ] \ ; , . / { } | : " < > ?"
</li>

And I want to check that each of the lines of text is shown in the page. Each line is not an element but a text object, so after some searching I came to this post but the answer did not work as expected :( I used this chunk of code:

JavascriptExecutor javascriptExecutor = (JavascriptExecutor)driver;
String value = (String)javascriptExecutor.executeScript("document.evaluate(\"//*[@id='mainContainer']/form/div/div/div/div[2]/div/ul/li/text()[2]\", document, null, XPathResult.STRING_TYPE, null ).stringValue;");
System.out.println(value);

The xpath works and points to the correct object when checking in on Chrome, but the print command prints null. I know I cannot access the text node as a web element, so I wanted to pick every line separately and compare them with the relevant, expected, string (using softassert).

What am I doing wrong?

Thank you very much

0

You are not returning from the execute script

JavascriptExecutor javascriptExecutor = (JavascriptExecutor)driver;
String value = (String)javascriptExecutor.executeScript("return document.evaluate(\"//*[@id='mainContainer']/form/div/div/div/div[2]/div/ul/li/text()[2]\", document, null, XPathResult.STRING_TYPE, null ).stringValue;");
System.out.println(value);
1
  • Thank you, that was it!!! Added the return before the document.evaluate and now I have a usable string. Thank you very much. Dec 9 '20 at 15:23

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.