简体   繁体   中英

NameError: name 'find_element' is not defined

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)

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM