How to find element by link text using selenium and python

Viewed 38

I am new to python. I am following some examples found online to learn it.

One of the examples is to use "selenium"

The example use "find_element_by_link_text", But it seems to be missing. I have to use "find_element" option.

I am using following code, but it is giving me error. The Chrome browser does get open error is in find_element part. Can anyone provide some advice on how to fix it?

from selenium import webdriver

browser = webdriver.Chrome()

browser.get("https://github.com")
signin_link = browser.find_element("LINK_TEXT", "Sign in")

print(signin_link)
1 Answers

Functions like find_element_by_x are not supported anymore since v4.0. Use the By class instead.

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

browser = webdriver.Chrome(ChromeDriverManager().install())

browser.get("https://github.com")
signin_link = browser.find_element(By.LINK_TEXT, "Sign in")
Related