【问题标题】:How to click the element with href attribute using Python Selenium如何使用 Python Selenium 单击具有 href 属性的元素
【发布时间】:2020-01-09 07:33:13
【问题描述】:

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"]')

【问题讨论】:

  • 您的图片不可读。请在问题中包含其文本

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

要在文本为 Booking without Fixed Instructor 的元素上 click(),您必须为 element_to_be_clickable() 诱导 WebDriverWait,您可以使用以下任一 @ 987654321@:

  • 使用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
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 2015-01-17
    • 2021-12-28
    • 1970-01-01
    • 2021-03-31
    相关资源
    最近更新 更多