Reputation: 5814
I'd like to select and click on the text 'Click Me Please' within this html link using python selenium.
The challenge is that there is no unique identifier on the item and it's located within a list.
The specific html code looks like this:
<li class="tile no-tile-status both price-suggestion-high"> <div class="date"> <span class="day-number"> <!-- react-text: 496 -->Click Me Please<!-- /react-text --> </span> </div> <div class="price"><span> $529 </span></div> <div class="price-suggestion-highlight"></div> </li>
I've tried the following methods with no success:
Attempt 1
driver.get("http://stackoverflowtest.site44.com/")
driver.find_element_by_link_text("Click Me Please").click()
#>>Message: u'no such element: Unable to locate element: {"method":"link text","selector":"Click Me Please"}\n (Session info: chrome=55.0.2883.95)\n (Driver info: chromedriver=2.24.417412 (ac882d3ce7c0d99292439bf3405780058fcca0a6),platform=Mac OS X 10.11.6 x86_64)'
Attempt 2
driver.get("http://stackoverflowtest.site44.com/")
driver.find_element_by_link_text("<!-- react-text: 496 -->Click Me Please<!-- /react-text -->").click()
#>>Message: u'no such element: Unable to locate element: {"method":"link text","selector":"<!-- react-text: 496 -->Click Me Please<!-- /react-text -->"}\n (Session info: chrome=55.0.2883.95)\n (Driver info: chromedriver=2.24.417412 (ac882d3ce7c0d99292439bf3405780058fcca0a6),platform=Mac OS X 10.11.6 x86_64)'
Note: I also want to be able to click on the other dates in the list such as the following html elements:
<li class="tile no-tile-status both price-suggestion-high"> <div class="date"><span class="day-number"><!-- react-text: 511 -->18<!-- /react-text --></span></div> <div class="price"><span>$533</span></div> <div class="price-suggestion-highlight"></div> </li>
<li class="tile no-tile-status both price-suggestion-high"> <div class="date"><span class="day-number"><!-- react-text: 525 -->20<!-- /react-text --></span></div> <div class="price"><span>$569</span></div> <div class="price-suggestion-highlight"></div> </li>
Upvotes: 1
Views: 163
Reputation: 2981
Usually I'd recommend using CSS selector as it's performance is better, but here you need to find it by it's text, so XPath will do the trick:
driver.find_element_by_xpath('//span[@class='day-number' and text()='Click Me Please']')
Just a note: clicking it doesn't do anything... :/ so don't expect anything to happen...
Upvotes: 1
Reputation: 13
Dave's answer is mostly correct, except you'll probably have to replace the //a
with //li
since it's referenced this way.
OK, looking at it more closely, the issue is that the link text can change as it includes "react-text", etc.
I'd suggest using -
.find_element_by_partial_link_text("Click Me Please")
Upvotes: 0