2
<div class='into'>
    <div class="state  " rel="AA" style="width:80px;">AA (1028)</div>
    <div class="state  " rel="BB" style="width:80px;">BB (307)</div>
</div>

I'd like to select one of elements rel="AA" or rel="BB" to click on it, tried several ways. The most usable idea was:

browser.find_element_by_xpath("//div[@class='into']/[text()='AA']").click()

However there is a number after the text what is various.

browser.find_element_by_xpath("//div[@class='into']/[rel='AA']").click()

And this not works.

1
  • Try @rel instead of just rel. Commented Feb 1, 2017 at 11:41

3 Answers 3

2

Use the following xpath

browser.find_element_by_xpath(".//div[@class='into']/div[@rel='CA']").click()

Also can use normalize-spacemethod to omit the spaces in your class name like below -

browser.find_element_by_xpath(".//div[normalize-space(@class)='state'][@rel='AA']").click()
Sign up to request clarification or add additional context in comments.

2 Comments

Unable to locate, also tried: //div[@class='into']/div[@class='state '][@rel='CA']
Yep! It works. Thank you! :) Could you clarify what . made?
0

If you need your XPath to match one of elements with attributes rel="AA" or rel="BB" (in case one of them might not be present on page) then try below:

browser.find_element_by_xpath("//div[@class='into']/div[@rel="AA" or @rel="BB"]").click()

Comments

0

If you want to use your example with text() then you could use either of the following:

browser.find_element_by_xpath("//div[@class='into']/div[contains(text(), 'AA')]").click()

or

browser.find_element_by_xpath("//div[@class='into']/div[starts-with(text(), 'AA')]").click()

otherwise use the answer given by @lauda and use @rel to declare it as an attribute

2 Comments

SyntaxError: The expression is not a legal expression. For both three tries
Ah, I forgot the second div, that is most likely the problem

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.