gpt4 book ai didi

http - 在 Python 中将 Firefox WebDriver 与 Selenium 连接时出现问题

转载 作者:可可西里 更新时间:2023-11-01 16:42:36 24 4
gpt4 key购买 nike

我在尝试启动 selenium firefox 驱动程序时遇到错误。似乎其他人在这一步遇到了障碍,网上没有现成的解决方案,所以希望这个问题能提供广泛的帮助。当通过 selenium 的驱动程序启动时,firefox 似乎无法建立 http 服务器接口(interface)。看来我可以从命令行运行 firefox 而没有错误。

我应该指定我正在通过 ssh 登录到 linux 容器来执行此操作。我在 Ubuntu 14.04 LTS (GNU/Linux 3.16.3-elastic x86_64) 上运行 python2.7。我安装了最新版本的 selenium (2.44),并且我使用的是 firefox 34.0。我正在使用 xvfb 来欺骗显示器。

下面是我的代码、错误日志和一些相关的源代码。

from selenium import webdriver
d = webdriver.Firefox()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
self.binary, timeout),
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
self.binary.launch_browser(self.profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 66, in launch_browser
self._wait_until_connectable()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 105, in _wait_until_connectable
raise WebDriverException("Can't load the profile. Profile "
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: %s If you specified a log_file in the FirefoxBinary constructor, check it for details.

由于超时,此处出现错误:

def _wait_until_connectable(self):
"""Blocks until the extension is connectable in the firefox."""
count = 0
while not utils.is_connectable(self.profile.port):
if self.process.poll() is not None:
# Browser has exited
raise WebDriverException("The browser appears to have exited "
"before we could connect. If you specified a log_file in "
"the FirefoxBinary constructor, check it for details.")
if count == 30:
self.kill()
raise WebDriverException("Can't load the profile. Profile "
"Dir: %s If you specified a log_file in the "
"FirefoxBinary constructor, check it for details.")
count += 1
time.sleep(1)
return True

在日志文件中:

tail -f logs/firefox_binary.log 
1418661895753 addons.xpi DEBUG checkForChanges
1418661895847 addons.xpi DEBUG No changes found
1418661895853 addons.manager DEBUG Registering shutdown blocker for XPIProvider
1418661895854 addons.manager DEBUG Registering shutdown blocker for LightweightThemeManager
1418661895857 addons.manager DEBUG Registering shutdown blocker for OpenH264Provider
1418661895858 addons.manager DEBUG Registering shutdown blocker for PluginProvider
System JS : ERROR (null):0 - uncaught exception: 2147746065
JavaScript error: file:///tmp/tmplkLsLs/extensions/fxdriver@googlecode.com/components/driver-component.js, line 11507: NS_ERROR_NOT_AVAILABLE: Component is not available'Component is not available' when calling method: [nsIHttpServer::start]
*** Blocklist::_preloadBlocklistFile: blocklist is disabled
1418661908552 addons.manager DEBUG Registering shutdown blocker for <unnamed-provider>

还有一点信息。早些时候,在 firefox 驱动程序初始化中,socket.bind(('127.0.0.1',0)) 失败并出现“无法分配请求的地址”错误。我将位置更改为 (0.0.0.0,0) 并编辑了我的/etc/hosts 中的本地主机条目,并且能够以这种方式进行绑定(bind)。不确定这是否会导致当前失败。

VV 根据路易斯的要求编辑 VV 。我指定了更改本地主机地址的两行。

def free_port(): """ 使用套接字确定空闲端口。 """ free_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) free_socket.bind(('0.0.0.0', 0)) # 从 127.0.0.1 改变 free_socket.listen(5) port = free_socket.getsockname()[1] free_socket.close() 返回端口

def is_connectable(port):
"""
Tries to connect to the server at port to see if it is running.

:Args:
- port: The port to connect.
"""
try:
socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_.settimeout(1)
socket_.connect(("0.0.0.0", port)) # changed again
socket_.close()
return True
except socket.error:
return False

这是来自 webdriver 的构造函数:

def __init__(self, firefox_profile=None, firefox_binary=None, timeout=30,
capabilities=None, proxy=None):

self.binary = firefox_binary
self.profile = firefox_profile

if self.profile is None:
self.profile = FirefoxProfile()

self.profile.native_events_enabled = (
self.NATIVE_EVENTS_ALLOWED and self.profile.native_events_enabled)

if self.binary is None:
self.binary = FirefoxBinary()

if capabilities is None:
capabilities = DesiredCapabilities.FIREFOX

if proxy is not None:
proxy.add_to_capabilities(capabilities)

RemoteWebDriver.__init__(self,
command_executor=ExtensionConnection("127.0.0.1", self.profile,
self.binary, timeout),
desired_capabilities=capabilities,
keep_alive=True)
self._is_remote = False

这是来自 extension_connector 的构造函数:

def __init__(self, host, firefox_profile, firefox_binary=None, timeout=30):
self.profile = firefox_profile
self.binary = firefox_binary
HOST = host
if self.binary is None:
self.binary = FirefoxBinary()

if HOST is None:
HOST = "127.0.0.1"

PORT = utils.free_port()
self.profile.port = PORT
self.profile.update_preferences()

self.profile.add_extension()

self.binary.launch_browser(self.profile)
_URL = "http://%s:%d/hub" % (HOST, PORT)
RemoteConnection.__init__(
self, _URL, keep_alive=True)

最佳答案

在发布我对路易斯评论的编辑时,我看到我的本地主机问题出现在其他位置,因为主机被硬编码了两次。我让我的服务器管理员解决了这个问题,将源代码中的所有内容改回 127,问题就解决了。感谢你提示我,@louis,我很抱歉我的问题不是很有趣。 SO 允许我在 2 天后接受我自己的回答。

关于http - 在 Python 中将 Firefox WebDriver 与 Selenium 连接时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27489228/

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