1

I am trying to click on this link:

<a href="updating/login.php" target="_top">Login</a>

with Selenium using Python. l've tried finding and clicking the element by text with this code, but it doesn't work:

elem = rd.driver.find_element_by_link_text("Login")
elem.click() 

How can this be achieved?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

To click on the WebElement with text as Login you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Login"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='updating/login.php']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@href='updating/login.php' and text()='Login']"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I tried your approach, but I got this error : `Traceback (most recent call last): File "C:/Users/ta-ph/Documents/Sites for success test/peopleFinder.py", line 13, in WebDriverWait(rd.driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//a[@href='updating/login.php' and text()='Login']"))).click() File "C:\Users\ta-ph\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: ` – Oluwatobi Elugbadebo Sep 23 '20 at 23:50