Clicking on a link using Selenium Python

1.6k Views Asked by At

Please need your support to click on a link in the following:

enter image description here

I tried the below code but doesn't work:

web.find_element(By.XPATH,'//*[@id="HUI_i_0"]/table/tbody/tr/td/a').click()

It gives me error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="HUI_i_0"]/table/tbody/tr/td/a"}
2

There are 2 best solutions below

0
On

The desired element is a dynamic element, so to click() on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using PARTIAL_LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "tbody > tr > td.treeNode > a"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.signin-btn.sbLoginBtn > span.btf-text"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'Subscriber Administration')]"))).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
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

5
On

Try to use link text to locate element:

web.find_element(By.LINK_TEXT,'Subscriber Administration').click()

If link is dynamic try to add wait