Home > Software engineering >  How to click on a link with Selenium with href="javascript:__doPostBack
How to click on a link with Selenium with href="javascript:__doPostBack

Time:04-09

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>

CodePudding user response:

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
    
  • Related