Home > OS >  NameError: name 'find_element' is not defined
NameError: name 'find_element' is not defined

Time:09-14

I'm trying to make a code with selenium but it shows the following error 'NameError: name 'find_element' is notdefined', can you help me?. Thankss

button_login = find_element(By.CSS_SELECTOR, '#loginForm > div > div:nth-child(3) > button')

button_login.click()

sleep(2)

CodePudding user response:

To use the find_element you first need to instantiate a webdriver object:

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get("your_url")
button_login = driver.find_element(By.CSS_SELECTOR, '#loginForm > div > div:nth-child(3) > button')
button_login.click()

time.sleep(2)

CodePudding user response:

find_element method should be applied on driver object of webdriver type.
So, instead of

button_login = find_element(By.CSS_SELECTOR, '#loginForm > div > div:nth-child(3) > button')

It should be something like

button_login = driver.find_element(By.CSS_SELECTOR, '#loginForm > div > div:nth-child(3) > button')

Where driver is previously defined and initialized in the way as following:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service)
  • Related