0

I am trying to select "seltarget" from a list of other options.

HTML:

<div class="filter-item" data-reactid=".3.1.0.0.1.0.0.3.0.0.0.3">seltarget</div>

When exported to a python file it recommends that I locate the element by xpath:

driver.find_element_by_xpath("//div[@id='quickswitcher']/div/nav/ul/li/div/div/menu/div[2]/div/div/div/div[5]").click()

This is working for the time being because "seltarget" just happens to be 5th in the list. The above line will select whichever option is 5th in the list. I want to be more specific and select the element "seltarget"

Guy
  • 46,488
  • 10
  • 44
  • 88
Chris
  • 123
  • 1
  • 1
  • 21
  • See http://stackoverflow.com/questions/5441680/css-selector-based-on-element-text you can select by the reactid or add a span to your seltarget – TheoretiCAL Mar 29 '16 at 17:37

2 Answers2

5

Locating by link_text works only with <a> tags. To locate this element you can use the class if it unique

driver.find_element_by_class_name('filter-item').click()

Or the data-reactid attribute

driver.find_element_by_css_selector('[data-reactid*="3.1.0.0.1.0.0.3.0.0.0.3"]').click()

To locate by the text you can also use css_selector

driver.find_element_by_css_selector('div:contains("seltarget")').click()

Or xpath

driver.find_element_by_xpath('//div[contains(text(), "seltarget")]').click()
Guy
  • 46,488
  • 10
  • 44
  • 88
  • I was able to select "seltarget" by locating it by it's `data-reactid`. I still would like to be able to select it by it's name: "seltarget" All of the options in the list have `class="filter-item"` Any other recommendations? – Chris Mar 29 '16 at 18:10
  • @Chris I added options by the text – Guy Mar 29 '16 at 18:15
1

If you want to find the div by its text, you can use the following.

driver.find_element_by_xpath("//div[text()='seltarget']")
David Baak
  • 944
  • 1
  • 14
  • 22