简体   繁体   中英

Python Selenium “NameError: name 'driver' is not defined”

I try to run a webdriver Selenium and select

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.select import Select

usernameStr = 'value'
passwordStr = 'value'


browser = webdriver.Chrome()
browser.get(('website'))

# fill in username and hit the next button

username = browser.find_element_by_id('username')
username.send_keys(usernameStr)

nextButton = browser.find_element_by_xpath('value')
nextButton.click()

# wait for transition then continue to fill items

username= WebDriverWait(browser, 2).until(
     EC.presence_of_element_located((By.ID, value')))
username.send_keys(usernameStr)

password = browser.find_element_by_id('value')
password.send_keys(passwordStr)


signInButton = browser.find_element_by_id('submitButton')
signInButton.click()

nextButton = browser.find_element_by_xpath('/html/body/app-root/div[2]/app-nav/div/div[1]/div[2]/div[1]/button')
nextButton.click()


el = driver.find_element_by_id('reason')
for option in el.find_elements_by_value('16'):

Problem is that i try to select a value from a list. (Everyhting works until this point):

el = driver.find_element_by_xpath('reason')
for option in el.find_elements_by_value('16'):

Error:

"NameError: name 'driver' is not defined"

I'm pretty noob at this, so sorry if this is an easy solution.

This error message...

"NameError: name 'driver' is not defined"

...implies that in your program you are referring to driver which is not defined within your program.


Details

You have initiated the WebDriver instance and referenced to it as browser almost throughout your program.

But in the line:

el = driver.find_element_by_xpath('reason')

you are trying to refer to a the instance driver which is not defined within your program. Hence the error.


Solution

Change driver to browser . So effectively your line of code will be:

el = browser.find_element_by_xpath('reason')

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