【问题标题】:How to address "Options is not defined error" in Python Selenium?如何解决 Python Selenium 中的“选项未定义错误”?
【发布时间】:2021-05-03 17:54:33
【问题描述】:

我最近遇到了一个问题,在使用 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 配置文件?

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping chrome-options


    【解决方案1】:

    有两件事。可能您还没有为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])
    

    【讨论】:

    • 好的,它成功了,但是我的个人资料没有加载,没有错误和正确的.add_argument,有什么常见的原因吗?
    • @martispy 加载 Chrome Profile 检查 this 和 this 讨论。
    【解决方案2】:

    您需要将Options 导入命名空间:

    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    

    【讨论】:

    • 好的,它成功了,但是我的个人资料没有加载,没有错误和正确的.add_argument,有什么常见的原因吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    相关资源
    最近更新 更多