1
<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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Jeon
  • 13
  • 5

2 Answers2

1

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.

Kamal
  • 2,384
  • 1
  • 13
  • 25
  • if it is just to click on a hyperlink, you can use linkText instead of xpath. Please read it carefully OP wants to click on third li tag not on anchor link. – cruisepandey Dec 05 '18 at 06:24
  • this code works. but i wonder why this code doesn't answer. is the code inefficient?? – Jeon Dec 05 '18 at 07:33
  • 1
    @Jeon Actually, this is an Answer but may have landed in the VLQ(very low quality) queue as the answer is a _code only_ answer without any textual context and may not be useful to the future readers. – undetected Selenium Dec 05 '18 at 07:50
  • @DebanjanB thx for answer, Then, is it better to use linkText? – Jeon Dec 05 '18 at 08:38
  • While this answer is also correct (+1) I have constructed a canonical answer. – undetected Selenium Dec 05 '18 at 10:10
0

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    it works perfectly, and i have question. Is it possible for equities in last two codes to be a partial path rather than a full path? – Jeon Dec 05 '18 at 10:24
  • @Jeon As a contributor on StackOverflow I don't have a visibility of the actual [HTML DOM](https://www.w3schools.com/js/js_htmldom.asp). So while answering I prefer to construct the locators as canonical as possible so that the locators are proved to be unique within the actual HTML. I firmly believe that helps an OP to understand how _Css_ and _Xpath_ are constructed. Once you are through with the concept you can start constructing the _Css_ and _Xpath_ with lesser attributes. So **yes**, you can drop some of the attributes from the _ancestor_ and _descendant_ till the locators are unique. – undetected Selenium Dec 05 '18 at 10:32