0

I am currently trying to learn selenium with python and I am having a lot of problems with the function:

browser.find_element_by_class_name('bob')

I have also tried using:

browser.find_element(By.CLASS_NAME, 'bob')

where 'bob' is the class name that I am trying to find, but it always returns the error code:

name "By" is not defined.

and if I remove "By" from the command, it always returns:

name "CLASS_NAME" is not defined.

So basically, I just want to call a class, but cannot seem to achieve this, and no matter where I look, I can't figure out how to write this. Sorry if I did anything wrong in this question, this is my first post to fullstack.

This is the complete code that I have used:

from selenium import webdriver
browser = webdriver.Edge()
browser.get('https://www.selenium.dev')
element = browser.find_element(By.CLASS_NAME, 'dropdown-item')

I have also tried:

from selenium import webdriver
browser = webdriver.Edge()
browser.get('https://www.selenium.dev')
browser.find_element_by_class_name('dropdown-item')

and I have tried both variations using "elements" instead of "element".

Can anyone help me out?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

From the error you're getting, the By class is not defined, and you are referencing an attribute of an undefined class. All you need to do is import the class and it should work

from selenium import webdriver
from selenium.webdriver.common.by import By

browser = webdriver.Edge()
browser.get('https://www.selenium.dev')
element = browser.find_element(By.CLASS_NAME, 'dropdown-item')
vizallati
  • 11
  • 3
0

The set of the following locator strategies:

  • CLASS_NAME
  • CSS_SELECTOR
  • ID
  • LINK_TEXT
  • NAME
  • PARTIAL_LINK_TEXT
  • TAG_NAME
  • XPATH

all of them are supported through the By class.


Solution

So when you execute the command:

browser.find_element(By.CLASS_NAME, 'bob')

you need to import By as follows:

from selenium.webdriver.common.by import By
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352