Mar*_*mbo 2 python firefox selenium geckodriver ubuntu-20.04
使用 Chrome (chromedriver) 非常简单:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option('detach', True)
Run Code Online (Sandbox Code Playgroud)
使用 Firefox (geckodriver) 则不会:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_experimental_option('detach', True) # Returns syntax error
Run Code Online (Sandbox Code Playgroud)
即使脚本结束也保持 Firefox 浏览器打开的等效语法是什么?
Chrome(chromedriver)和Firefox(geckodriver)的底层结构是不同的。
例如chromedriver中存在add_experimental_option
geckodriver中不存在此add_experimental_option,因此这就是您收到错误的原因。
我浏览了各种geckodriver文档,但没有看到与chromedriver 中的此文档类似的参考。
请注意,下面的 options_spec.rb 代码来自 selenium 的 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'})
Run Code Online (Sandbox Code Playgroud)
下面的代码来自文件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'})
Run Code Online (Sandbox Code Playgroud)
下面的代码来自文件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'})
Run Code Online (Sandbox Code Playgroud)
根据这 3 个选项文件中的字典,人们会假设{'detach': True}不是 geckodriver 中的选项。
Python selenium结构中geckodriver的options.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
Run Code Online (Sandbox Code Playgroud)
当查看 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}
Run Code Online (Sandbox Code Playgroud)
因此 {'detach': True} 是geckodriver 中的一个选项,因此您应该能够通过以下方式设置该选项:
firefox_options.set_preference('detach', True)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2858 次 |
最近记录: |