Reputation: 13
<ul id='pairSublinksLevel1' class='arial_14 bold newBigTabs'>...<ul>
<ul id='pairSublinksLevel2' class='arial_12 newBigTabs'>
<li>...</li>
<li>...</li>
<li>
<a href='/equities/...'> last data </a> #<-- HERE
</li>
<li>...</li>
Question is how can i get click third li tag ??
In my code
xpath = "//ul[@id='pairSublinksLevel2']"
element = driver.find_element_by_xpath(xpath)
actions = element.find_element_by_css_selector('a').click()
code works partially. but i want to click third li tag. The code keeps clicking on the second tag.
Upvotes: 1
Views: 206
Reputation: 193188
As per the HTML you have shared you can use either of the following solutions:
Using link_text
:
driver.find_element_by_link_text("last data").click()
Using partial_link_text
:
driver.find_element_by_partial_link_text("last data").click()
Using css_selector
:
driver.find_element_by_css_selector("ul.newBigTabs#pairSublinksLevel2 a[href*='equities']").click()
Using xpath
:
driver.find_element_by_xpath("//ul[@class='arial_12 newBigTabs' and @id='pairSublinksLevel2']//a[contains(@href,'equities') and contains(.,'last data')]").click()
Reference: Official locator strategies for the webdriver
Upvotes: 0
Reputation: 2554
Try
driver.find_element_by_xpath("//ul[@id='pairSublinksLevel2']/li[3]/a").click()
EDIT: Thanks @DebanjanB for suggestion:
When you get the element with xpath //ul[@id='pairSublinksLevel2']
and search for a
tag in its child elements, then it will return the first match(In your case, it could be inside second li
tag). So you can use indexing as given above to get the specific numbered match. Please note that such indexing starts from 1 not 0.
Upvotes: 1