Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath selenium multi/and operators

Tags:

selenium

xpath

Could anybody help how to solve this dilemma

I have this code:
  <div>
    <a href="/cars/102392-2">Link</a>
    <span class="make">Chevrolet</span><br>
    <span class="year">1956</span><br>
    <span class="price">$20,000</span><br>
  </div>

  <div>
    <a href="/cars/152933-11">Link</a>
    <span class="make">Ford</span><br>
    <span class="year">1958</span><br>
    <span class="price">$21,000</span><br>
  </div>

I need get the link for example Fords with the year greater then 1950.

Presently, I am using following xpath:

//*[text()='Ford' and .//text()>'1950']//parent::a 

And this doesn't work! Have you any idea ?

like image 753
TheRutubeify Avatar asked Jan 18 '26 13:01

TheRutubeify


1 Answers

This is one possible XPath :

//div[span/text()='Ford' and span/text()>1950]/a

Basically the XPath check if div has child span with text equals 'Ford' and another child span with value greater than 1950. Then from such div that match the two criteria above, return child a element.

demo

Better yet, only check span with class 'make' for manufacturer and span with class 'year' for manufacturing year :

//div[span[@class='make']='Ford' and span[@class='year']>1950]/a
like image 144
har07 Avatar answered Jan 21 '26 07:01

har07



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!