Home > front end >  Get the text before & after of hyperlink using Xpath
Get the text before & after of hyperlink using Xpath

Time:05-04

I am trying to find cases using Xpath/Selenium where there is no white-space before the hyperlink.

e.g.

<p>Click on this<a href="#">link</a>to access the data</p>

This renders as Click on thislinkto access the data

Problem : Locate all the <a> elements and test if they have white-space before and after

Is there any elegant way to get the text before/after of anchor? I am thinking of using XPath query such as //a/parent::* which returns <p> element but the <a> tag is not separated. Can I somehow get the immediate text before/after the anchor tag?

CodePudding user response:

Since you're using selenium, I'm assuming xpath 1.0.

This should select a elements that don't have a preceding or following space...

//a[substring(preceding-sibling::text()[1],string-length(preceding-sibling::text()[1]) )!=' ' or substring(following-sibling::text()[1],1,1)!=' ']

CodePudding user response:

You can try this

//a/..


Output:


Click on thislinkto access the data

Output-2

  //a/../a

link

  • Related