Windows上Python中的Selenium和GhostDriver

Windows上Python中的Selenium和GhostDriver,python,selenium,phantomjs,ghostdriver,Python,Selenium,Phantomjs,Ghostdriver,这是一个令人尴尬的问题,因为这似乎是一个错误几率很小的问题。我不认为这会很难,但我已经坚持了近3个小时,这让我头疼。我读过几十个stackoverflow线程和Google线程 我已经安装了PhantomJS,将它添加到我的系统变量路径中,并且它在命令行中正常工作。我之前还使用easy_install安装了Selenium 我得到的错误是: __init__ C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\w

这是一个令人尴尬的问题,因为这似乎是一个错误几率很小的问题。我不认为这会很难,但我已经坚持了近3个小时,这让我头疼。我读过几十个stackoverflow线程和Google线程

我已经安装了PhantomJS,将它添加到我的系统变量路径中,并且它在命令行中正常工作。我之前还使用easy_install安装了Selenium

我得到的错误是:

__init__    C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriver\phantomjs\webdriver.py   50      

start   C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriver\phantomjs\service.py 66      

WebDriverException: Message: 'Unable to start phantomjs with ghostdriver.' ; Screenshot: available via screen               
这是我的密码:

from selenium import webdriver

driver = webdriver.PhantomJS(executable_path="C:\Python27\misc\phantomjs\phantomjs.exe")
我还尝试:

from selenium import webdriver

driver = webdriver.PhantomJS()
我收到了相同的错误消息。这一定是我做错的简单的事情。如有任何意见或回答,我将不胜感激

Windows 7 64位
Python2.7这可能是您的版本问题,但由于我刚刚在Windows 7 PC上完成了设置,没有出现任何问题,因此我将在这里分享我的“旅程”

首先,我更习惯于Mac/Linux终端,拥有python包管理器
pip
对我来说至关重要。安装并添加
后;c:\Python27
我注意到
pip
不包含在低于2.7.9的Python版本中,所以我选择了。之后我添加了
;c:\Python27\Scripts
到我的路径

之后,获取python包
selenium
就像在cmd中键入以下内容一样简单:

pip install selenium
然后我从下载了
phantomjs-1.9.7-windows.zip,解压缩后放在这里:

C:\Python27\misc\phantomjs-1.9.7-windows\phantomjs.exe
在那里,我有了一个适用于Windows7的Python 2.7/Selenium Webdriver/PhantomJS示例

from selenium import webdriver
import os

phantomjs_path = "C:\Python27\misc\phantomjs-1.9.7-windows\phantomjs.exe"

browser = webdriver.PhantomJS(executable_path=phantomjs_path, service_log_path=os.path.devnull)
browser.set_window_size(1400, 1000)

browser.get("https://stackoverflow.com/")

print browser.title

请注意,我在函数
webdriver.PhantomJS()
中添加了参数
service\u log\u path=os.path.devnull
,以防止PhantomJS在正在执行的python文件目录中创建ghostdriver.log。

我在Windows Server 2012 R2上运行python 3.4时遇到同样的问题。PhantomJS未能创建ghostdriver.log文件。我按照以下步骤为自己解决了问题:

  • 确保phantomjs.exe在文件属性|安全选项卡上未显示“已阻止”,并将其作为独立应用程序运行以进行确认
  • 删除了同一目录下ghostdriver.log文件的旧副本
  • 从控制台运行python REPL,同时检查实例化驱动程序的代码是否被成功调用

    browser = webdriver.PhantomJS(executable_path='phantomjs.exe', desired_capabilities=argdc, service_args=svc_args)
    

您是否有其他同名文件或目录,或者您命名为与
phantomjs
相同的编码文件(如..phantomjs.py),然后将其重命名为其他文件。我希望它能工作

是的,我复制了路径并粘贴了它,没有错误。我也只是在命令行中运行了它,它变成了“phantomjs>”。所以一切都很好。我想这可能是版本不匹配或什么的?这真让我难堪。路径是:C:\Python27\misc\phantomjs\I无法复制它。请尝试放入其他路径,然后重试。错误信息说路径错误。是的,我在发布之前已经试过了。真奇怪。当我说版本不匹配时,我在考虑Selenium和PhantomJS,但它们今天都是新安装的。这越来越奇怪了。我将every/更改为/,然后在pyscripter中运行未保存的python脚本,它给了我一个错误:IOError:[Errno 13]权限被拒绝:“ghostdriver.log”。我认为这是进步。然后我保存了它,错误又回来了。这个问题与路径的引用有关,或者与路径的解释方式有关。