Python Selenium Firefox geckodriver。从终端分离浏览器

问题描述 投票:0回答:3

使用 Chrome (chromedriver) 非常简单:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option('detach', True)

使用 Firefox (geckodriver) 否:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_experimental_option('detach', True)  # Returns syntax error

即使脚本结束也保持 Firefox 浏览器打开的等效语法是什么?

python selenium firefox geckodriver ubuntu-20.04
3个回答
4
投票

Chrome(chromedriver)和Firefox(geckodriver)底层结构不同。

例如,add_experimental_option存在于chromedriver

这个 add_experimental_optiongeckodriver 中不存在,所以这就是您收到错误的原因。

我浏览了各种 geckodriver 文档,但没有看到与 chromedriver 中类似的参考。

请注意下面的 options_spec.rb 代码来自 Ruby 源代码 硒的代码。

下面的代码来自文件 options_spec.rb,它是 chromedriver 的 selenium 源代码的一部分。 请注意字典中的 detach: true

opts = Options.new(browser_version: '75',
                   platform_name: 'win10',
                   accept_insecure_certs: false,
                   page_load_strategy: 'eager',
                   unhandled_prompt_behavior: 'accept',
                   strict_file_interactability: true,
                   timeouts: {script: 40000,
                              page_load: 400000,
                              implicit: 1},
                   set_window_rect: false,
                   args: %w[foo bar],
                   prefs: {foo: 'bar'},
                   binary: '/foo/bar',
                   extensions: ['foo.crx', 'bar.crx'],
                   encoded_extensions: ['encoded_foobar'],
                   foo: 'bar',
                   emulation: {device_name: :bar},
                   local_state: {foo: 'bar'},
                   detach: true,
                   debugger_address: '127.0.0.1:8181',
                   exclude_switches: %w[foobar barfoo],
                   minidump_path: 'linux/only',
                   perf_logging_prefs: {enable_network: true},
                   window_types: %w[normal devtools],
                   'custom:options': {foo: 'bar'})

下面的代码来自文件 options_spec.rb,它是 geckodriver 的 selenium 源代码的一部分。注意字典中没有 detach: true

opts = Options.new(browser_version: '66',
                   platform_name: 'win10',
                   accept_insecure_certs: false,
                   page_load_strategy: 'eager',
                   unhandled_prompt_behavior: 'accept',
                   strict_file_interactability: true,
                   timeouts: {script: 40000,
                              page_load: 400000,
                              implicit: 1},
                   set_window_rect: false,
                   args: %w[foo bar],
                   binary: '/foo/bar',
                   prefs: {foo: 'bar'},
                   foo: 'bar',
                   profile: profile,
                   log_level: :debug,
                   'custom:options': {foo: 'bar'})

下面的代码来自文件 options_spec.rb,它是 edgedriver selenium源代码的一部分。注意字典中有一个 detach: true

opts = Options.new(browser_version: '75',
                   platform_name: 'win10',
                   accept_insecure_certs: false,
                   page_load_strategy: 'eager',
                   unhandled_prompt_behavior: 'accept',
                   strict_file_interactability: true,
                   timeouts: {script: 40000,
                              page_load: 400000,
                              implicit: 1},
                   set_window_rect: false,
                   args: %w[foo bar],
                   prefs: {foo: 'bar'},
                   binary: '/foo/bar',
                   extensions: ['foo.crx', 'bar.crx'],
                   encoded_extensions: ['encoded_foobar'],
                   foo: 'bar',
                   emulation: {device_name: :bar},
                   local_state: {foo: 'bar'},
                   detach: true,
                   debugger_address: '127.0.0.1:8181',
                   exclude_switches: %w[foobar barfoo],
                   minidump_path: 'linux/only',
                   perf_logging_prefs: {enable_network: true},
                   window_types: %w[normal devtools],
                   'custom:options': {foo: 'bar'})

根据这 3 个选项文件中的字典,人们会假设 {'detach': True} 不是 geckodriver 中的选项。

Python

selenium 结构中的 geckodriveroptions.py 与 Ruby 文件 options_spec.rb. 不同

def preferences(self) -> dict: """:Returns: A dict of preferences.""" return self._preferences def set_preference(self, name: str, value: Union[str, int, bool]): """Sets a preference.""" self._preferences[name] = value
在 Mozilla 的 gecko-dev GitHub 存储库中查看 Python 

geckodriver 的源代码时,我发现我可以查询预定义的首选项和功能。

from selenium.webdriver.firefox.options import Options firefox_options = Options() print(firefox_options.arguments) # output ['--test-type', '--ignore-certificate-errors', '--disable-infobars', '--disable-extensions', '--disable-popup-blocking'] print(firefox_options.capabilities) # output {'browserName': 'firefox', 'marionette': True, 'acceptInsecureCerts': True} print(firefox_options.preferences) # output {'detach': True}
因此 {'detach': True} 是 

geckodriver 中的一个选项, 因此您应该能够以这种方式设置该选项:

firefox_options.set_preference('detach', True)
    

2
投票
如果您从不调用

driver.close()

,浏览器窗口将保持打开状态,您可以根据需要检查/使用它。你试过这个吗?


0
投票
你只需要这个,感谢@生活很复杂

因此 {'detach': True} 是 geckodriver 中的一个选项,因此您应该能够以这种方式设置该选项:

firefox_options.set_preference('分离', True)

© www.soinside.com 2019 - 2024. All rights reserved.