How can I use getText to capture text on lines which aren’t tagged?

Tags: , , , ,



I have the following web element, which looks like this when inspecting the web page element in the browser:

<p class="mb-2">
    <strong>Your reference is</strong>
    ": "
    "xxxxxxxxx"
</p>

I’ve not seen text separated by lines and no web elements before.

I am using IntelliJ and Serenity, and a line which is attempting to getText for the string of numbers (xxxxxxxxx), of which the length may change. That code, down in the Page Object, looks like this:

return referenceNumber.getText();

There have also been attempts to split the text (return referenceNumber.getText().split(” “)[4];) because we only want the referenceNumber.

However, the issue I have is that the code to return the text only captures the first line:

Your reference is

So I guess no amount of splitting will work anyway at this stage. Does anyone have any ideas how I can, essentially, identify the row of text which doesn’t seem to have it’s own xpath, or somehow include it within the xpath for the WebElement, which is currently quote ugly (I plan to clean that up once the referenceNumber is found):

//*[@id='root']/div/main/div/div[1]/div[1]/p[2]/strong

I’ve tried various combinations of text(), but not yet found a way to capture anything past that first line within the strong tag.

Answer

According to your sample, the reference number is a text node, child of the p element not the strong.

You can use this XPath to locate it :

//p[./strong[contains(.,"Your reference is")]]

Then use getText and clean the result with functions (regex, split,…) to extract the reference number.

I don’t know if IntelliJ supports it, but you can do this with XPath with a one liner :

translate(substring-after(//p[./strong[contains(.,'Your reference is')]],":"),'"','')

Output :

xxxxxxxxx


Source: stackoverflow