Davi Luis
Davi Luis

Reputation: 436

How to click the page link with selenium python

My error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"entrar"}

I have the html below:

<a role="menuitem" href="/login" class="_yce4s5">
  <div class="_hgs47m">
     <div class="_10ejfg4u">
       <div>
         Entrar
       </div>
     </div>
 </div> 
</a>

I click on the then link entrar, the following code in selenium with python:

driver.implicitly_wait(10)
element = driver.find_element_by_link_text('Entrar')
element.click()

But It raises this NoSuchElementException.

Upvotes: 1

Views: 1359

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193338

To click the page link with text as Entrar as the element is a dynamic element ou have to induce WebDriverWait for the desired element to be clickable and you can use either of the following solution:

  • Using CSS_SELECTOR:

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

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@role='menuitem' and @href='/login']//div[normalize-space()='Entrar']"))).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
    

Upvotes: 1

Mike C.
Mike C.

Reputation: 1921

You might try driver.find_element_by_partial_link_text('entrar') because there is probably other text in your link. I like getting element by xpath. It's pretty easy. If you have Firefox just get the add-on xpath finder to find the xpath to any element on a web page.

Upvotes: 1

Evgenii Bazhanov
Evgenii Bazhanov

Reputation: 926

Did you try to use Upper case for the first letter? like 'Entrar' instead of 'entrar'

Upvotes: 1

Related Questions