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

1.1k Views Asked by At

I tried to click link from selenium.webdriver but I got it nothing. Can please help with this issue

Page contains the following elements:

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

My target to click:

"m_m_cBody_bdy_uc_tbl$Edit","13911"

Please note that I have many text links with

Details

is not a unique element on my page

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

There are 1 best solutions below

3
On BEST ANSWER

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 XPATH and normalize-space():

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

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