Chrome(chromedriver)和Firefox(geckodriver)的底层结构不同。
例如 add_experimental_option 存在于 chromedriver
geckodriver 中不存在此 add_experimental_option,所以这就是您收到错误的原因。
我查看了各种 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 结构中 geckodriver 的 options.py 与 Ruby 文件 options_spec.rb 不同em>。
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)