1

I have the below element in the page I'm 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][1], 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 soft assert).

What am I doing wrong?

1 Answer 1

1

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, 2020 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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