首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium + Geckodriver故障排除

Selenium + Geckodriver故障排除
EN

Stack Overflow用户
提问于 2017-11-29 07:05:31
回答 1查看 854关注 0票数 1

我正在用Python中的selenium使用Firefox壁虎驱动程序来抓取论坛的帖子标题,并且遇到了一个我似乎搞不懂的问题。

代码语言:javascript
复制
~$ geckodriver --version
geckodriver 0.19.0

The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.

This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.

我正试图从论坛上收集几年过去的帖子标题,我的代码在一段时间内运行良好。我坐下来看了20到30分钟,它做的正是它应该做的。然后我开始写剧本,然后上床睡觉,当我第二天早上醒来的时候,我发现它已经处理了22,000个帖子。我目前正在抓取的网站每页有25篇文章,所以它在崩溃前通过了~880个单独的URL。

当它崩溃时,它会抛出以下错误:

代码语言:javascript
复制
WebDriverException: Message: Tried to run command without establishing a connection

最初,我的代码如下所示:

代码语言:javascript
复制
FirefoxProfile = webdriver.FirefoxProfile('/home/me/jupyter-notebooks/FirefoxProfile/')
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True

driver = webdriver.Firefox(FirefoxProfile, capabilities=firefox_capabilities)
for url in urls:
    driver.get(url)
    ### code to process page here ###
driver.close()

我也试过:

代码语言:javascript
复制
driver = webdriver.Firefox(FirefoxProfile, capabilities=firefox_capabilities)
for url in urls:
    driver.get(url)
    ### code to process page here ###
    driver.close()

代码语言:javascript
复制
for url in urls:
    driver = webdriver.Firefox(FirefoxProfile, capabilities=firefox_capabilities)
    driver.get(url)
    ### code to process page here ###
    driver.close()

我在所有三个场景中都得到了相同的错误,但是只有在它成功运行了很长一段时间之后,我才知道如何确定它失败的原因。

如何确定在成功处理数百url's之后为什么会出现此错误?或者,对于处理这么多页面,我没有遵循Selenium/Firefox的最佳实践吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-29 08:36:29

所有三个代码块都接近完美,但都有以下一些小缺陷:

您的第一个代码块是:

代码语言:javascript
复制
driver = webdriver.Firefox(FirefoxProfile, capabilities=firefox_capabilities)
for url in urls:
    driver.get(url)
    ### code to process page here ###
driver.close()

代码块看起来很有希望,没有一个问题。在最后一步中,按照Best Practices,我们必须调用driver.quit()而不是driver.close(),这将阻止驻留在System Memory.中的悬空的webdriver实例。你可以找到driver.quit()driver.close() here的不同之处。

第二个代码块是:

代码语言:javascript
复制
driver = webdriver.Firefox(FirefoxProfile, capabilities=firefox_capabilities)
for url in urls:
    driver.get(url)
    ### code to process page here ###
    driver.close()

此块容易出错。一旦执行进入for()循环并在url上工作,我们将最终关闭该url因此,当执行开始第二次迭代的循环时,driver.get(url)上的脚本错误,因为没有Active Browser Session.

您的第三个代码块是:

代码语言:javascript
复制
for url in urls:
    driver = webdriver.Firefox(FirefoxProfile, capabilities=firefox_capabilities)
    driver.get(url)
    ### code to process page here ###
    driver.close()

代码块看起来与第一个代码块几乎没有相同的问题。在最后一步中,我们必须调用driver.quit()而不是driver.close(),这将阻止驻留在System Memory.中的悬挂的webdriver实例。当悬空的webdriver实例创建家务并在某个时候继续占用端口时,WebDriver无法找到空闲端口或无法打开新的Browser Session/Connection.。因此,您可以将错误视为WebDriverException: Message:尝试运行命令而不建立连接

解决办法:

按照driver.quit(),尝试调用WebDriver而不是driver.close(),打开一个新的WebDriver实例和一个新的Web Browser Session.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47546903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档