2

I've a structure like this:

<ul> 
 <div class="someclass"> 
  <li>  
    <img/> 
    <img/>
    <a> <span> Text 1 </span> <a/>
  </li>
  <li>  
    <img/> 
    <img/>
    <a> <span> Text 2 </span> <a/>
  </li>
 </div>
<ul/>

I want to search "Text 2" and get back to 1st "<img />" of that and I can't proceed further than this //div[@class='someclass']/li//following-sibling::a/span[text()='Text 2']

Abhinav Kushagra
  • 176
  • 1
  • 2
  • 15

2 Answers2

2

To search for the element with text as Text 2 and referencing it to locate the first <img/> tag you can use the following based solution:

  • xpath:

    "//div[@class='someclass']//li//a/span[normalize-space()='Text 2']//preceding::img[2]"
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You can get to the "Text 2" and then you can use preceding with a[1] to get the "Text 1".
Your xpath should be like:

//a[contains(text(),'Text 2')]//preceding::a[1]
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20