1

I have the following HTML code:

<div class="t2-selector">  
    <div>  
        <div class="inactive">USA</div>  
        <div class="inactive">Google</div>  
        <div class="inactive">Microsoft</div>  
        <div class="inactive">Apple</div>
    </div>
    <div>
        <div class="">Europe</div>
        <div class="selected">BT</div>
    </div>
    <div>
        <div class="">Indices</div>
        <div>Vodafone</div>
        <div>Renault</div>
    </div>
    <div>
        <div class="">Currencies</div>
        <div>EUR/USD</div>
        <div>GBP/USD</div>
        <div>USD/JPY</div>
        <div>USD/CHF</div>
        <div>AUD/USD</div>
        <div>USD/CAD</div>
   </div>  

I can select a group by Xpath xpath = "//div[contains(text(),'Currencies')]"
What I need is to select a child in Currencies list by it's number.
I need something like CSS div:nth-child(2) but I can't use CSS here since CSS doesn't really support selecting an element by text.
So is there nth-child analog for Xpath?

4
  • I'm not sure why you are not using classes? And what are you planning to do with the selected element? Assuming you want to do styling [as you added the css tag] and didn't just add the empty class tag for fun couldn't this be a more simple solution? Commented Jan 12, 2016 at 8:56
  • 1
    Do you know Selenium WebDriver what is? Commented Jan 12, 2016 at 13:53
  • I do not, i just realize i didn't even really look at the tags, i am sorry! I guess this technology requiers xpath selectors, not css ones. You simply added the css tag for reference to the nth-child selector then i assume? Commented Jan 12, 2016 at 13:59
  • Selenium WebDriver is used for automation web testings. In the code above you can see no classes, they used for inactive indication only. So I need the Xpath or CSS expression to select the desired element and click on it, maybe extract it's attribute, never changing it. And as about the CSS tag I used - you are right, it is because I was looking for nth-child analog. Commented Jan 12, 2016 at 14:24

2 Answers 2

3

You can use

//div[contains(text(),'Currencies')]/following-sibling::div[1]

Please note that the index starts from 1 not 0.

Check the following link : http://www.w3schools.com/xsl/xpath_axes.asp

Sign up to request clarification or add additional context in comments.

Comments

0

Using descendant -

//div[contains(text(),'Currencies')]/descendant::div[2]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.