gpt4 book ai didi

python - 将 -profile 参数添加到 python selenium 选项时,Firefox 连接被拒绝

转载 作者:行者123 更新时间:2023-12-05 06:11:39 24 4
gpt4 key购买 nike

我正在尝试将选项添加到 python 中我的 Firefox selenium 驱动程序的启动到 capture crash data .我用来启动驱动程序的函数如下所示:

import os 
from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FFOptions
from selenium.webdriver.firefox.webdriver import WebDriver as FirefoxWebDriver
import tempfile
SELENIUM_TEMP_DIR = os.path.abspath('./data/raw')


def get_local_firefox_driver():

os.environ['MOZ_FORCE_DISABLE_E10S'] = '1'
ff_options = FFOptions()

# Add custom crash dump folder
profile_temp = tempfile.mkdtemp(".selenium", dir=SELENIUM_TEMP_DIR)
print("*** Using profile: {}".format(profile_temp))
ff_options.add_argument("-profile")
ff_options.add_argument(profile_temp)

ff_profile = webdriver.FirefoxProfile()
# set some privacy settings
ff_profile.set_preference("places.history.enabled", False)
ff_profile.set_preference("privacy.clearOnShutdown.offlineApps", True)
ff_profile.set_preference("privacy.clearOnShutdown.passwords", True)
ff_profile.set_preference("privacy.clearOnShutdown.siteSettings", True)
ff_profile.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
ff_profile.set_preference("signon.rememberSignons", False)
ff_profile.set_preference("network.cookie.lifetimePolicy", 2)
ff_profile.set_preference("network.dns.disablePrefetch", True)
ff_profile.set_preference("network.http.sendRefererHeader", 0)
ff_profile.set_preference("permissions.default.image", 2)

driver = webdriver.Firefox(firefox_profile=ff_profile, options=ff_options)
return driver

当我尝试使用此版本的函数启动驱动程序时,出现此错误:

Traceback (most recent call last):
File "/Users/username/project_name/error_demo.py", line 2, in <module>
driver = get_local_firefox_driver(headless=False)
File "/Users/username/project_name/src/selenium_helper.py", line 129, in get_local_firefox_driver
driver = webdriver.Firefox(firefox_profile=ff_profile, options=ff_options, capabilities=ff_capabilities)
File "/Users/username/miniconda3/envs/live_auction/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
keep_alive=True)
File "/Users/username/miniconda3/envs/live_auction/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/Users/username/miniconda3/envs/live_auction/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Users/username/miniconda3/envs/live_auction/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/username/miniconda3/envs/live_auction/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: connection refused

在崩溃期间,Firefox 实际上打开并且看起来像这样: enter image description here

如果我注释掉旨在捕获崩溃数据的代码部分

# Add custom crash dump folder
profile_temp = tempfile.mkdtemp(".selenium", dir=SELENIUM_TEMP_DIR)
print("*** Using profile: {}".format(profile_temp))
ff_options.add_argument("-profile")
ff_options.add_argument(profile_temp)

我没有收到任何错误,Firefox 浏览器打开如下:

enter image description here

需要如何修改 get_local_firefox_driver() 才能使 Firefox 和 geckodriver 正确启动并能够捕获崩溃数据?

最佳答案

我认为这样的事情应该可行:

import tempfile

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

# Custom profile folder to keep the minidump files
profile = tempfile.mkdtemp(".selenium")
print("*** Using profile: {}".format(profile))

# Use the above folder as custom profile
opts = Options()
opts.add_argument("-profile")
opts.add_argument(profile)
opts.binary = "/Applications/Firefox.app/Contents/MacOS/firefox"

driver = webdriver.Firefox(options=opts,
# hard-code the Marionette port so geckodriver can connect
service_args=["--marionette-port", "2828"])

# Your test code which crashes Firefox

如果这不起作用,我建议您查看 official documentation

关于python - 将 -profile 参数添加到 python selenium 选项时,Firefox 连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63906413/

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