0

how can i click on the "NATIONAL" text with python selenium

<div class="ellipsis__content"> <ul class="breadcrumb_new" id="breadcrumb"> <li> <span>NATIONAL</span> </li> </ul> </div> 

I tried these below things,

find_element_by_xpath("//div[@id='breadcrumb']/li/span") find_element_by_css_selector("#breadcrumb > li > span") 

Both the methods not working, any idea here

0

    3 Answers 3

    1

    Try this one, should work for you:

    ele=driver.find_element_by_xpath("//*[@id='breadcrumb']//span") ele.click() 
    1
    • Found the initial element using //*[@id='breadcrumb'] which means I will search the entire HTML source for an element having this ID; predominantly done by //*, which in this case also indicates that my target is under this umbrella and then directly target the span tag using //span.CommentedSep 29, 2021 at 17:53
    1

    Already this question has an answered but adding couple of points.

    I tried these below things,

    find_element_by_xpath("//div[@id='breadcrumb']/li/span") 

    The idbreadcrumb is part of ul tag but you are referring div tag. It should work if try as below.

    find_element_by_xpath("//ul[@id='breadcrumb']/li/span") 

    find_element_by_css_selector("#breadcrumb > li > span")

    There is nothing wrong with CSS locator. It should work still if you are seeing issue you may need to debug and see or try by adding some explicit value.

    enter image description here

      0

      May be you need to wait till element is rendered properly.

      Code trial 1 :

      time.sleep(5) driver.find_element_by_xpath("//div[@id='breadcrumb']/li/span").click() 

      Code trial 2 :

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='breadcrumb']/li/span"))).click() 

      PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

      Steps to check:

      Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.