如何使用带有 href="javascript:__doPostBack 的 Selenium 单击 link

How to click on a link with Selenium with href="javascript:__doPostBack

我试图从 selenium.webdriver 单击 link,但我什么也没得到。可以请帮助解决这个问题

页面包含以下元素:

<a href="javascript:__doPostBack("m_m_cBody_bdy_uc_tbl$Edit","13911")"> Details </a>

我的点击目标:

"m_m_cBody_bdy_uc_tbl$Edit","13911"

请注意,我有很多文本 links

Details

不是我页面上的唯一元素

<a href="javascript:__doPostBack("m_m_cBody_bdy_uc_tbl$Edit","41946")"> 
Details </a>

点击需要诱导的元素WebDriverWait for the and you can use either of the following :

  • 使用 XPATHnormalize-space():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[normalize-space()='Details' and contains(@href, '13911')]"))).click()
    
  • 使用 XPATHcontains():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'Details') and contains(@href, '13911')]"))).click()
    
  • 注意:您必须添加以下导入:

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