简体   繁体   中英

How to click the element with href attribute using Python Selenium

The href is like this:

href="../s-reminderNotice.asp?fname=b%2D3c%2DpLessonBooking%2Easp%3Flimit%3Dpl"

在此处输入图片说明 I just want to click() this text link on a website. My code is:

driver.find_element_by_xpath('//a[@href="../s-reminderNotice.asp?fname=b%2D3c%2DpLessonBooking%2Easp%3Flimit%3Dpl"]')

To click() on the element with text as Booking without Fixed Instructor you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :

  • Using LINK_TEXT :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Booking without Fixed Instructor"))).click()
  • Using PARTIAL_LINK_TEXT :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Booking without Fixed Instructor"))).click()
  • Using CSS_SELECTOR :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.sidelinkbold[href*='LessonBooking'][onmouseover*='Practical Training Booking']"))).click()
  • Using XPATH :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='sidelinkbold' and contains(@href,'LessonBooking')][contains(@onmouseover, 'Practical Training Booking')]"))).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