繁体   English   中英

如何使用 Python Selenium 单击具有 href 属性的元素

[英]How to click the element with href attribute using Python Selenium

href 是这样的:

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

在此处输入图片说明 我只想在网站上click()此文本链接。 我的代码是:

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

要将带有文本的元素click()Booking without Fixed Instructor ,您必须为element_to_be_clickable()引入WebDriverWait并且您可以使用以下任一定位器策略

  • 使用LINK_TEXT

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

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

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.sidelinkbold[href*='LessonBooking'][onmouseover*='Practical Training Booking']"))).click()
  • 使用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()
  • 注意:您必须添加以下导入:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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