1

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.

6
  • Can you share the url ? Commented Dec 13, 2018 at 22:57
  • Please provide the full error traceback Commented Dec 13, 2018 at 22:58
  • Because your text is in the <div> tag which contains no link. You would want to search for the <a tag above through another method (xpath/class/bs4/requests) Commented Dec 13, 2018 at 22:58
  • Do what @The BrownBatman says. Or you can find the <div> as you are currently doing and then go get the <a> tag by going to a parent node twice. Commented Dec 13, 2018 at 23:00
  • driver.find_element_by_link_text('Entrar') # Capital E Commented Dec 13, 2018 at 23:11

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you but it is not the solution.
@DaviLuis, try driver.find_element_by_partial_link_text('Entrar'). The anchor tag contains other tags apart from the text.
1

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.

Comments

1

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
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.