首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GhostDriver和PhantomJS错误:“无法连接到Selenium”

GhostDriver和PhantomJS错误:“无法连接到Selenium”
EN

Stack Overflow用户
提问于 2013-12-01 13:51:30
回答 2查看 5.4K关注 0票数 4

我正在运行Selenium和PhantomJS来将搜索词输入到网站中,并检索每个搜索词的命中数量。我必须这样做130,000+次,所以代码已经很好地运行了一天,直到突然程序崩溃,出现以下错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "CBBPlyNwsScrape.py", line 82, in <module>
     browser = webdriver.PhantomJS()
  File "/Library/Python/2.7/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 50, in __init__
     self.service.start()
  File "/Library/Python/2.7/site-packages/selenium/webdriver/phantomjs/service.py", line 69, in start
     raise WebDriverException("Can not connect to GhostDriver")
selenium.common.exceptions.WebDriverException: Message: 'Can not connect to GhostDriver' 

我在Mac和Python 2.7.3上运行它。我安装了Selenium和PhantomJS的最新版本。有人能告诉我发生了什么事吗?为什么GhostDriver工作了这么长时间却突然停止工作?

ghostdriver.log文件中,它包含的内容如下:

代码语言:javascript
复制
PhantomJS is launching GhostDriver...
[ERROR - 2013-12-01T05:14:34.491Z] GhostDriver - Main - Could not start Ghost Driver => {
  "message": "Could not start Ghost Driver",
  "line": 82,
  "sourceId": 4445044288,
  "sourceURL": ":/ghostdriver/main.js",
  "stack": "Error: Could not start Ghost Driver\n    at :/ghostdriver/main.js:82",
  "stackArray": [
     {
        "sourceURL": ":/ghostdriver/main.js",
        "line": 82
     }
  ]
}

谢谢

EN

回答 2

Stack Overflow用户

发布于 2014-03-11 17:20:57

安装最新的phantom js修复了这个错误,这发生在默认的ubuntu 12.04 phantomjs destro上

票数 3
EN

Stack Overflow用户

发布于 2014-01-03 10:44:57

我也有同样的问题。我不知道为什么程序在调用phantomJS why驱动程序时会遇到问题,但答案是编写一个简单的异常WebDriverException。下面的代码帮我解决了这个问题

代码语言:javascript
复制
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException, WebDriverException
import unittest, time, re, urllib2

f = open("mother.txt","r") #opens file with name of "test.txt"
l = "1"
m = "2"
n = "3"
aTuple = ( l, m, n ) # create tuple
e = int(0)


for line in f:          
    e += 1
    try:
        h = str(e)
        j = line
        g = open("yes4/" + h + ".txt","w") #opens file with name of "test.txt"
        for item in aTuple:
            driver = webdriver.PhantomJS('phantomjs')
            base_url = j + item
            verificationErrors = []
            accept_next_alert = True
            driver.get(base_url)
            elem=driver.find_element_by_id("yelp_main_body")
            source_code=elem.get_attribute("outerHTML").encode('utf-8').strip()
            g.write(source_code)
            driver.quit()
    except WebDriverException:
        print "e"       
        h = str(e)
        j = line
        g = open("yes4/" + h + ".txt","w") #opens file with name of "test.txt"
        for item in aTuple:
            driver = webdriver.PhantomJS('phantomjs')
            base_url = j + item
            verificationErrors = []
            accept_next_alert = True
            driver.get(base_url)
            elem=driver.find_element_by_id("yelp_main_body")
            source_code=elem.get_attribute("outerHTML").encode('utf-8').strip()
            g.write(source_code)
            driver.quit()
    else:
        print h
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20309170

复制
相关文章

相似问题

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