【问题标题】:Why doesn't chrome extension work on selenium after add more options?添加更多选项后,为什么 chrome 扩展不能在 selenium 上工作?
【发布时间】:2021-12-15 21:04:33
【问题描述】:

我正在抓取一些需要身份验证的带有 selenium 和代理的网站,因此我使用 this 扩展来设置代理的凭据,此时一切顺利。

def get_chromedriver(use_proxy=False, user_agent=None):
    path = os.path.abspath(
        os.path.join(os.path.dirname(__file__), './drivers/current/', 'chromedriver.exe'))
    chrome_options = webdriver.ChromeOptions()
    if use_proxy:
        pluginfile = 'proxy_auth_plugin.zip'

        with zipfile.ZipFile(pluginfile, 'w') as zp:
            zp.writestr("manifest.json", manifest_json)
            zp.writestr("background.js", background_js)
        chrome_options.add_extension(pluginfile)
    if user_agent:
        chrome_options.add_argument('--user-agent=%s' % user_agent)
    return driver

但是,我也需要设置以下选项

  chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
    chrome_options.add_experimental_option('useAutomationExtension', False)
    chrome_options.add_argument("--disable-blink-features=AutomationControlled")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--incognito")

整个函数看起来是这样的,但是当我执行下面的代码时没有设置代理,而是使用我的公共地址。我的代码有什么问题?

def get_chromedriver(use_proxy=False, user_agent=None):
    path = os.path.abspath(
        os.path.join(os.path.dirname(__file__), './drivers/current/', 'chromedriver.exe'))
    chrome_options = webdriver.ChromeOptions()
    if use_proxy:
        pluginfile = 'proxy_auth_plugin.zip'

        with zipfile.ZipFile(pluginfile, 'w') as zp:
            zp.writestr("manifest.json", manifest_json)
            zp.writestr("background.js", background_js)
        chrome_options.add_extension(pluginfile)
    if user_agent:
        chrome_options.add_argument('--user-agent=%s' % user_agent)
    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
    chrome_options.add_experimental_option('useAutomationExtension', False)
    chrome_options.add_argument("--disable-blink-features=AutomationControlled")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--incognito")
    driver = webdriver.Chrome(executable_path=path,
                              chrome_options=chrome_options)
    return driver

【问题讨论】:

  • chrome_options = webdriver.ChromeOptions()from selenium.webdriver.chrome.options import Options 导入时应为chrome_options = Options() 查看documentation
  • @It_is_Chris 我只是绑它没有成功
  • 你是怎么调用函数的?参数设置为FalseNone
  • 是的,没错。我刚刚发现隐身参数正在禁用扩展!

标签: python selenium google-chrome-extension


【解决方案1】:

已解决 经过一些调试并逐个尝试参数后,我发现chrome_options.add_argument("--incognito") 参数阻止了浏览器中的所有扩展。被禁用后,程序再次运行。如果其他人想使用隐身参数添加更多扩展,请尝试this solution

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多