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?

Alex Kulinkovich
6272 gold badges8 silver badges18 bronze badges
asked Dec 9, 2020 at 12:59

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);
answered Dec 9, 2020 at 14:22
1
  • Thank you, that was it!!! Added the return before the document.evaluate and now I have a usable string. Thank you very much. Commented Dec 9, 2020 at 15:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.