Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解决Python Selenium中的“选项未定义错误”?_Python_Selenium_Selenium Webdriver_Web Scraping_Chrome Options - Fatal编程技术网

如何解决Python Selenium中的“选项未定义错误”?

如何解决Python Selenium中的“选项未定义错误”?,python,selenium,selenium-webdriver,web-scraping,chrome-options,Python,Selenium,Selenium Webdriver,Web Scraping,Chrome Options,我最近遇到了一个问题,在使用Selenium时,我想使用默认的chrome浏览器配置文件,所以它已经登录了,但在google上搜索时,它给了我一个基本如下的代码: chrome_options = Options() chrome_options.add_argument("[path to the profile]") driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=[path

我最近遇到了一个问题,在使用Selenium时,我想使用默认的chrome浏览器配置文件,所以它已经登录了,但在google上搜索时,它给了我一个基本如下的代码:

chrome_options = Options()
chrome_options.add_argument("[path to the profile]")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=[path to the webdriver])
我得到的错误是:

NameError: name 'Options' is not defined
如何解决这个问题?也许有更好的方法加载chrome配置文件?

您需要将选项导入到名称空间:

从selenium.webdriver.chrome.options导入选项 chrome_选项=选项 您需要将选项导入命名空间:

从selenium.webdriver.chrome.options导入选项 chrome_选项=选项
有两件事。可能您尚未导入选项所需的模块。因此,要使用选项的实例,必须包括以下导入:

此外,chrome_选项已被弃用,您必须改用选项。因此,您的有效代码块将是:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("[path to the profile]")
driver = webdriver.Chrome(options=chrome_options, executable_path=[path to the webdriver])

有两件事。可能您尚未导入选项所需的模块。因此,要使用选项的实例,必须包括以下导入:

此外,chrome_选项已被弃用,您必须改用选项。因此,您的有效代码块将是:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("[path to the profile]")
driver = webdriver.Chrome(options=chrome_options, executable_path=[path to the webdriver])

好的,它工作了,但是我的配置文件没有加载,没有错误和正确。添加参数,有什么常见原因吗?好的,它工作了,但是我的配置文件没有加载,没有错误和正确。添加参数,有什么常见原因吗?好的,它工作了,但是我的配置文件没有加载,没有错误和正确。添加参数,有什么常见原因吗?@martispy用于加载检查和讨论。好的,它工作了,但我的配置文件没有加载,没有错误和正确。添加_参数,有什么常见原因吗?@martispy用于加载检查和讨论。