简体   繁体   English

Python Selenium “名称错误:未定义名称‘驱动程序’”

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

I try to run a webdriver Selenium and select我尝试运行网络驱动程序 Selenium 和 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.问题是我尝试 select 一个列表中的值。 (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. ...意味着在您的程序中您指的是未在您的程序中定义的driver程序。


Details细节

You have initiated the WebDriver instance and referenced to it as browser almost throughout your program.您已经启动了WebDriver实例,并且几乎在整个程序中都将其作为browser引用。

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.您正在尝试引用未在程序中定义的实例driver程序。 Hence the error.因此错误。


Solution解决方案

Change driver to browser .driver更改为browser So effectively your line of code will be:因此,您的代码行将是:

el = browser.find_element_by_xpath('reason')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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