尝试方法一:

打开hosts文件配置

cat /etc/hosts

添加

127.0.0.1 localhost

重新运行

尝试方法二:

1,抛开服务,直接调用phantomjs定位问题

由于我是从服务的日志中看到报错的,可能是因为phantomjs这个出错了,于是我就在88服务器上直接运行如个代码:

[root(songxianfeng01@58OS)@bjm6--88ershouservices]# python

>>> from selenium import webdriver
>>>dr=webdriver.PhantomJS('phantomjs')

结果报如下错误:

Traceback (most recent call last):
File"/usr/local/python2.7/lib/python2.7/site-packages/selenium-3.6.0-py2.7.egg/selenium/webdriver/phantomjs/webdriver.py",
line , in __init__self.service.start()
File
"/usr/local/python2.7/lib/python2.7/site-packages/selenium-3.6.0-py2.7.egg/selenium/webdriver/common/service.py",
line , in start
raise WebDriverException("Can not connect to the
Service %s" % self.path)
selenium.common.exceptions.WebDriverException:
Message: Can not connect to the Service
phantomjsdriver

没有看出直接原因是什么,于是就采取常规的办法:重新安装相关软件

于是就:

./easy_install -m selenium

将selenium重新卸载了再安装,同时也将phantomjs删除了

rm -rf /usr/local/phantomjs

rm -f /usr/local/bin/phantomjs

而后按网上提供的方案重新安装完成!

结果发现:问题依然存在,没有什么用处!

2,查看源码,精准进行定位

重新安装没有任何效果后,我就将上面的两个文件webdriver.py和service.py下载了下来。

(1)先去service.py中搜索报错的代码段:

count= 0
while True:
self.assert_process_still_running()
if self.is_connectable():
break
count+= 1
time.sleep(1)
if count
==30:
raise WebDriverException("Can not connect to the Service %s"
% self.path)

直接原因是count到了30了,相当于尝试了30次self.is_connectable都失败了。
(2)查看函数is_connectable这个函数是什么意思?

def is_connectable(self):
return utils.is_connectable(self.port)

看来直接原因是utils.is_connectable这个函数报错的,不过这个port的值是什么呢?我好像没有传这个值啊?

(3)查看self.port的值

在webdirver.py中,port默认值为0:

def __init__(self,executable_path="phantomjs",
port=0,
desired_capabilities=DesiredCapabilities.PHANTOMJS,service_args=None,service_log_path=None):

在调用service.py文件中的类的时候,如果port值等于0,则随机获取一个端口

class Service(object):

def __init__(self,executable,
port=0,
log_file=DEVNULL, env=None,
start_error_message=""):
self.path
=executable
self.port
=port
if self.port==
0:
self.port
=utils.free_port()

于是我们就直接验证utils相关的代码:

>>> from selenium.webdriver.common
import utils
>>> utils.free_port()
49439
>>>utils.is_connectable(49439)
False

发现无论端口号是多少,都会返回false,所以self.is_connectable函数永远都是失败,尝试30次之后就会报异常。

(4)查看utils.is_connectable函数源码,看能否找到原因?

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(("127.0.0.1", port))
      socket_.close()
      return True
   except socket.error:
       return False  

看出最终的原因应该是socket连接出错了。于是就手功尝试一下:

>>> import socket
>>> socket_
=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>>socket_.connect(("127.0.0.1",49439))
Traceback (most recent call last):
File "", line 1, inFile"/usr/local/python2.7/lib/python2.7/socket.py",
line 228, in meth
return
getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection
refused
>>>

通过查询socket 111错误与phantomjs,webdriver相关的内容,发现可能是webdirver与phantomjs不匹配的问题。可是我不记得原来的webdirver的版本是什么了?

3,最终尝试,解决问题

通过上面的分析,最终问题定位是:可能是有人重新安装了服务器的python selenium了,造成的不兼容问题。

解决方案:安装最新的selenium和phantomjs.

(1)selenium更新

通过访问相关的官网,发现selenium的最新版本是3.7.0,而通过easy_install安装的是3.6.0,果断更新之!

https://pypi.python.org/pypi/selenium/#downloads

(2)phantomjs更新

因为phantomjs是刚刚安装过的,是最新的版本2.1.1,下载地址是:

http://phantomjs.org/download.html

(3)全部更新后再次尝试

>>> from selenium import webdriver

>>> dr = webdriver.PhantomJS('phantomjs')

>>>

>>>

没有任何报错,开心一下。

phantomjs Can not connect to the Service phantomjs错误的更多相关文章

  1. iTunes Connect TERMS OF SERVICE

    iTunes Connect TERMS OF SERVICE THESE TERMS OF SERVICE CONSTITUTE A LEGAL AGREEMENT BETWEEN YOU AND ...

  2. java.lang.RuntimeException: Fail to connect to camera service

    玩自定义照相机的时候出现了:java.lang.RuntimeException: Fail to connect to camera service 讲过百度和Google后知道是权限少加了.(试验 ...

  3. py+selenium运行时报错Can not connect to the Service IEDriverServer.exe

    问题: 运行用例时,出现报错(host文件已加入127.0.0.1 localhost): raise WebDriverException("Can not connect to the ...

  4. 【打印机】无法连接 fail to connect to server cups 服务器错误 打印机

       打印机     fail to connect to server cups 服务器错误兆芯 打印机 连不上了####message可知 服务fail## systemd: cups.servi ...

  5. 【Oracle】ORA-00257:archiver error. Connect internal only, until freed 错误的处理方法

    archive log 日志已满ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法 1. 用sys用户登录  s ...

  6. ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法

    转帖:原文地址http://blog.csdn.net/panys/article/details/3838846 archive log 日志已满ORA-00257: archiver error. ...

  7. LR回放https协议脚本失败: 错误 -27778: 在尝试与主机“www.baidu.com”connect 时发生 SSL 协议错误

    今天用LR录制脚本协议为https协议,回放脚本时出现报错: Action.c(14): 错误 -27778: 在尝试与主机"www.baidu.com"connect 时发生 S ...

  8. 关于ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法

    转 关于ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法 2016年03月31日 10:14:59 阅读数:1 ...

  9. DEBUG : Eclipse Debug 时出现 Cannot connect to VM select failed错误

    Eclipse在执行Debug操作时, 出现“Eclipse Debug 时出现 "Cannot connect to VM select failed"”错误, 在网上查找该错误 ...

随机推荐

  1. warning: implicit declaration of function 'getMyfilename' [-Wimplicit-function-declaration]|

    我在main后面定义了getMyfilename()函数,然后就报出这个warning. 在main前声明一下就好了.

  2. 分布式Ruby解决之道

    其实用Druby很久了,今天需要完成一个进程数据同步的机制,我需要的不是运行速度快,不是用 linux / mac 下的扩展,而是独立,快速开发效率,方便最简单的Ruby环境可运行,可以吗? DRb( ...

  3. 解决idea的项目启动报404的问题

    最近在学习IDEA,由于之前一直使用的是Eclipse,所以初次接触IDEA就接触到了不少的坑,其中最让人头疼的大概就是如何让IDEA顺利的启动起来了,这方面我就不细讲了,网上已经有了不少的教程,稍后 ...

  4. 计算机网络-TCP/IP HTTP Conclusion

    1.1OSI 与 TCP/IP 各层的结构 1.2 三次握手和四次挥手,TCP为什么三次握手,四次挥手 在第一次消息发送中,A随机选取一个序列号作为自己的初始序号发送给B:第二次消息B使用ack对A的 ...

  5. vue2.0-基于elementui换肤[自定义主题]

    0. 直接上 预览链接 vue2.0-基于elementui换肤[自定义主题] 1. 项目增加主题组件 在项目的src/components下添加skin文件夹 skin文件获取地址 2. 项目增加自 ...

  6. zookeeper 分布式管理

    分布式框架: Zookeeper与paxos算法 一.            zookeeper是什么 官方说辞:Zookeeper 分布式服务框架是Apache Hadoop 的一个子项目,它主要是 ...

  7. Python新手入门学习常见错误

    当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的一些让你程序 crash 的运行时错误. 1)忘记在 if , elif , else , for , ...

  8. Angular6.0发布

    Angular v6 新版本重点关注工具链以及工具链在 Angular 中的运行速度问题. Angular v6 是统一整体框架.Material 和 CLI 三大 Angular 组件的第一个版本, ...

  9. DDD实战进阶第一波(九):开发一般业务的大健康行业直销系统(实现经销商上下文仓储与领域逻辑)

    上篇文章主要讲述了经销商上下文的需求与POCO对象,这篇文章主要讲述该界限上下文的仓储与领域逻辑的实现. 关于界限上下文与EF Core数据访问上下文参考产品上下文相应的实现,这里不再累述. 因为在经 ...

  10. Selenium调用webdriver.chrome()出错

    问题描述: 今天因为在学习要使用selenium这个python库,我下载好了selenium,并且也Import成功了,但是在我使用webdirver.chorme()时,却提示PATH路径中没有c ...