I have below html code
<a class = sidetoolsdivider>
<div class = sideone > Test 1 </div>
<div class = sidetwo> </div>
</a>
<a class = sidetoolsdivider>
<div class = sideone > Test 2 </div>
<div class = sidetwo> </div>
</a>
...............
Here I need to find xpath locator of class sidetwo which has text Test1. There are many such similar classes hence you can differentiate between different only based on element text
CodePudding user response:
The xpath would be something like below:
Since the element depends on the text, can make use of text attribute for the same.
//div[text()='Text1']/following-sibling::div
Or
//div[contains(text(),'Text1')]/following-sibling::div
Or
//div[contains(text(),'Text1')]/following-sibling::div[@class='sidetwo']
Link to refer - Link
CodePudding user response:
This gets you the correct 'a'. Find an 'a' which contains the right div of sideone (note the .//, find a Child which is) "//a[.//div[ @class='sideone" and text()='Test 1']"
Then just get the side two, complete xPath "//a[.//div[ @class='sideone" and text()='Test 1']//div[@class='sidetwo']"
Works even if there is more text inside the entire 'a' and stuff gets complex with more elements inside.