简体   繁体   中英

Selenium chromedriver cannot click href link

I am writing a script to automate data collection and was having trouble clicking a link. The website is behind a login, but I navigated that successfully. I ran into problems when trying to navigate to the download page. This is in python using chrome webdriver.

I have tried using:

find_element_by_partial_link_text('stuff').click()
find_element_by_xpath('stuff').click()
#and a few others

I get iterations of following message when I try a few of the selector statements.

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"download"}
  (Session info: chrome=88.0.4324.182)

Html source I'm trying to use is:

<a routerlink="/download" title="Download" href="/itron-mvweb/download"><i class="fa fa-lg fa-fw fa-download"></i><span class="menu-item-parent">Download</span></a>

Thank you!

This is caused by a typo. Download is case-sensitive, make sure you capitalize the D !

To click on the element with text as Download you can use either of the following Locator Strategies :

  • Using css_selector :

     driver.find_element(By.CSS_SELECTOR, "a[title='Download'][href='/itron-mvweb/download'] span.menu-item-parent").click()
  • Using xpath :

     driver.find_element(By.XPATH, "//a[@title='Download' and @href='/itron-mvweb/download']//span[@class='menu-item-parent' and text()='Download']").click()

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :

  • Using CSS_SELECTOR :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Download'][href='/itron-mvweb/download'] span.menu-item-parent"))).click()
  • Using XPATH :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Download' and @href='/itron-mvweb/download']//span[@class='menu-item-parent' and text()='Download']"))).click()
  • Note : You have to add the following imports:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

References

You can find a couple of relevant discussions on NoSuchElementException in:

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