0

I have a problem. I want to find the login page of several websites using Selenium. To do this, I try to go to the page and click the button with text such as "sign in", "login", and so on. This worked for Netflix. Example:

Code Website: Netflix.com

<a href="/login" class="authLinks redButton" data-uia="header-login-link">Sign In</a>

My Code to detect the button:


result = "https://www.microsoft.com"

driver.get(result)

elem = driver.find_element_by_link_text('Sign In').click

print(driver.current_url) # returns the website: https://www.netflix.com/de-en/login

Now I try this with the website Microsoft.com. Website code as image:

WEBSITE CODE MICROSOFT

If I now change my contains query to:

driver.find_element_by_link_text('Sign in').click # small "in"

Selenium does not find any element. I have tried many different options that Selenium offers such as:

  1. find_element_by_id
  2. find_element_by_name
  3. find_element_by_link_text
  4. ...

eg: [https://selenium-python.readthedocs.io/locating-elements.html]

My goal is to use the textual information like "sign in, login" etc. to find the button and press it.

I would be very grateful for your help

2 Answers 2

2

See the issue here is with Netflix.com , you have a HTML like this :

<a href="/login" class="authLinks redButton" data-uia="header-login-link">Sign In</a>

see the tag, it is a, which is an anchor tag in HTML.

find_element_by_link_text, or find_element_by_partial_link_text look for text between anchor tag.

But when you go to Microsoft.com

<div class="mectrl_header_text mectrl_truncate">Sign in</div>

Sign in is wrapped inside div. so find_element_by_link_text or find_element_by_partial_link_text will not work.

instead you can try with below xpath :-

//div[text()='Sign in']

in code :-

driver.find_element_by_xpath("//div[text()='Sign in']").click()

or you can give it a try with Explicit waits as well :-

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Sign in']"))).click()
Sign up to request clarification or add additional context in comments.

Comments

0

As described by cruisepandey for these two particular cases, find_element_by_link_text will work in some cases but in some other cases will not.
While locating element by it's text will work for all these case.
So I never use find_element_by_link_text or by partial text methods, just using find_element_by_xpath method based on element's text and it works fine.
In your case you can use

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Sign in']"))).click()

This will be more simple, stable and reliable.

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.