【问题标题】:Python Selenium Firefox geckodriver. Detach browser from terminalPython Selenium Firefox geckodriver。从终端分离浏览器
【发布时间】:2021-08-26 10:07:27
【问题描述】:

使用 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


    【解决方案1】:

    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 结构中 geckodriveroptions.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)
    

    【讨论】:

    • 这是正确的命令,但它不起作用。它需要一个错误报告。谢谢。
    【解决方案2】:

    如果您从未致电driver.close(),浏览器窗口将保持打开状态,您可以随意检查/使用它。你试过吗?

    【讨论】:

    • 我的目标是使用GeckoDriver突然关闭终端必须和使用ChromeDriver的效果一样,即不关闭浏览器,而是ChromeDriver和GeckoDriver有两个相反的行为突然关闭终端的。
    猜你喜欢
    • 2017-12-20
    • 2020-06-04
    • 2019-03-13
    • 2018-01-02
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2020-10-08
    相关资源
    最近更新 更多