0

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Aman Ghuman
  • 1
  • 1
  • 2

2 Answers2

1

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.


Solution

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Most `find_element_by_*("text")` is now removed from selenium as of version 4.5, replaced by `find_element(By.*,"text")` – MortenB Nov 08 '22 at 09:58
0

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"
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Are you sure `driver.find_element_('Webinar')` will work? assuming you remove the trailing `_`, `'Webinar'` will populate the `by` parameter, not the value. – Guy Mar 21 '22 at 08:22
  • @Guy: thnx man! It was my bad, typo :( – cruisepandey Mar 21 '22 at 08:25
  • @cruisepandey thanks for help tried using this way earlier also but did not worked for me I know sounds crazy why it doesn't work at my end it gives error NameError: name 'By' is not defined and yeah i am using selenium 4.1 on mac – Aman Ghuman Mar 22 '22 at 16:06