简体   繁体   中英

Python selenium find element by xpath or css selector

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

Try this one, should work for you:

ele=driver.find_element_by_xpath("//*[@id='breadcrumb']//span")
ele.click()

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.

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 id breadcrumb 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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM