简体   繁体   中英

How can I click a href button using python 3.8 selenium?

  1. The problem: I'm trying to click this href here:

在此处输入图片说明

  1. Fail attempts: I tried to use these to no avail

    driver.find_element_by_link_text('Join').click() driver.find_element_by_partial_link_text('href').click()

You can use xpath instead of link text.

driver.find_element_by_xpath('//a[contains(text(), "John"]').click()

Or add space in front of John .

driver.find_element_by_link_text(' Join').click()

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

  • Using partial_link_text :

     driver.find_element_by_partial_link_text("Join").click()
  • Using xpath :

     driver.find_element_by_xpath("//a[contains(., 'Join')]").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 PARTIAL_LINK_TEXT :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Join"))).click()
  • Using XPATH :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'Join')]"))).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

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