Reputation: 1
I am new to selenium whenever I try to find elements using id
, name
, xpath
or anything it gives the same error
from selenium import webdriver
driver = webdriver.Safari()
driver.get('https://www.lambdatest.com/blog/selenium-safaridriver-macos/')
driver.find_element(By.Link_Text,'Webinar' )
It shows error by underlining By. I tried writing
driver.find_element_by_id('Webinar')
But it does not go through I tried different websites and with different elements like class, id but seems like there is a problem with finding elements by any method is there something that needs to installed to make it work?
Upvotes: 0
Views: 4544
Reputation: 193308
You have to take care of a couple of things here:
The supported locator strategy should be LINK_TEXT
instead of Link_Text
The link_text should be Webinars
instead of Webinar
.
Effectively, your line of code should be:
driver.find_element(By.LINK_TEXT, 'Webinars' )
However, within the webpage there are multiple elements with innerText
as Webinars
. So you need to use a locator strategies which identifies the element uniquely within the DOM.
Ideally, to locate the clickable element from the menu container with text as Webinars
you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR
:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.menu-menu-1-container a[href='https://www.lambdatest.com/webinar/']")))
Using XPATH
:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='menu-menu-1-container']//a[text()='Webinars']")))
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
Reputation: 29382
This
driver.find_element(By.Link_Text,'Webinar')
should be
driver.find_element(By.LINK_TEXT, 'Webinar')
Also, Import this from selenium.webdriver.common.by import By
If you are on Selenium4 then you should not really use:
driver.find_element_by_id('Webinar')
use this instead:
driver.find_element(By.ID, 'Webinar')
If you take a look at the source code, you'd see:
class By(object):
"""
Set of supported locator strategies.
"""
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
Upvotes: 0