gpt4 book ai didi

Python Selenium Firefox 壁虎驱动程序。从终端分离浏览器

转载 作者:行者123 更新时间:2023-12-02 18:41:51 25 4
gpt4 key购买 nike

使用 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 浏览器保持打开状态的等效语法是什么?

最佳答案

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

例如 add_experimental_option 存在于 chromedriver enter image description here

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

enter image description here

我查看了各种 geckodriver 文档,但没有看到与此类似的来自 chromedriver 的引用。

enter image description here

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

下面的代码来自文件 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)

关于Python Selenium Firefox 壁虎驱动程序。从终端分离浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67903537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com