2

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

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 配置文件?

4

2 回答 2

3

有两件事。可能您还没有为Options. 因此,要使用您的实例,Options您必须包含以下内容import

from selenium.webdriver.chrome.options import Options

此外,chrome_options已弃用,您必须options改用。因此,您的有效代码块将是:

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])
于 2021-01-29T22:03:08.593 回答
1

您需要导入Options命名空间:

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
于 2021-01-29T20:07:07.310 回答