Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
selenium python不使用phantomjs_Python_Selenium Webdriver_Phantomjs - Fatal编程技术网

selenium python不使用phantomjs

selenium python不使用phantomjs,python,selenium-webdriver,phantomjs,Python,Selenium Webdriver,Phantomjs,我已经通过python安装了带有npm和selenium python的phantomjs 在无头Firefox上一切正常,但在phantomjs上则不然 这是代码 In [1]: from selenium import webdriver In [2]: browser = webdriver.PhantomJS() In [3]: browser.get('http://www.google.com/') ---------------------------------------

我已经通过python安装了带有npm和selenium python的phantomjs

在无头Firefox上一切正常,但在phantomjs上则不然

这是代码

In [1]: from selenium import webdriver

In [2]: browser = webdriver.PhantomJS()

In [3]: browser.get('http://www.google.com/')

---------------------------------------------------------------------------
CannotSendRequest                         Traceback (most recent call last)
<ipython-input-5-a95426e05380> in <module>()
----> 1 browser.get('http://www.google.com/')

/home/asit/pyrepo/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc in get(self, url)
    174         Loads a web page in the current browser session.
    175         """
--> 176         self.execute(Command.GET, {'url': url})
    177 
    178     @property

/home/asit/pyrepo/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc in execute(self, driver_command, params)
    160 
    161         params = self._wrap_value(params)
--> 162         response = self.command_executor.execute(driver_command, params)
    163         if response:
    164             self.error_handler.check_response(response)

/home/asit/pyrepo/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.pyc in execute(self, command, params)
    353         path = string.Template(command_info[1]).substitute(params)
    354         url = '%s%s' % (self._url, path)
--> 355         return self._request(url, method=command_info[0], data=data)
    356 
    357     def _request(self, url, data=None, method=None):

/home/asit/pyrepo/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.pyc in _request(self, url, data, method)
    381             headers["Authorization"] = "Basic %s" % auth
    382 
--> 383         self._conn.request(method, parsed_url.path, data, headers)
    384         resp = self._conn.getresponse()
    385         statuscode = resp.status

/usr/lib/python2.7/httplib.pyc in request(self, method, url, body, headers)
    956     def request(self, method, url, body=None, headers={}):
    957         """Send a complete request to the server."""
--> 958         self._send_request(method, url, body, headers)
    959 
    960     def _set_content_length(self, body):

/usr/lib/python2.7/httplib.pyc in _send_request(self, method, url, body, headers)
    984             skips['skip_accept_encoding'] = 1
    985 
--> 986         self.putrequest(method, url, **skips)
    987 
    988         if body and ('content-length' not in header_names):

/usr/lib/python2.7/httplib.pyc in putrequest(self, method, url, skip_host, skip_accept_encoding)
    854             self.__state = _CS_REQ_STARTED
    855         else:
--> 856             raise CannotSendRequest()
    857 
    858         # Save the method we use, we need it later in the response phase

CannotSendRequest: 
[1]中的
:来自selenium导入webdriver
在[2]中:browser=webdriver.PhantomJS()
在[3]:browser.get('http://www.google.com/')
---------------------------------------------------------------------------
CannotSendRequest回溯(最近一次呼叫上次)
在()
---->1.获取http://www.google.com/')
/get中的home/asit/pyrepo/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc(self,url)
174在当前浏览器会话中加载网页。
175         """
-->176 self.execute(Command.GET,{'url':url})
177
178@property
/home/asit/pyrepo/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc in execute(self,driver_命令,参数)
160
161参数=自身值(参数)
-->162响应=自命令执行器执行(驱动程序命令,参数)
163如果答复:
164自我。错误处理程序。检查响应(响应)
/home/asit/pyrepo/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.pyc in execute(self、command、params)
353 path=string.Template(命令信息[1]).substitute(参数)
354 url=“%s%s%”(自身url,路径)
-->355返回自我请求(url,方法=命令信息[0],数据=数据)
356
357 def_请求(self、url、data=None、method=None):
/home/asit/pyrepo/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote\u connection.pyc in\u请求(self、url、数据、方法)
381头[“授权”]=“基本%s”%auth
382
-->383自我连接请求(方法、解析的url.path、数据、标题)
384 resp=self.\u conn.getresponse()
385状态代码=相应状态
/请求中的usr/lib/python2.7/httplib.pyc(self、方法、url、正文、标题)
956 def请求(self、方法、url、body=None、headers={}):
957“向服务器发送完整的请求。”“”
-->958自我发送请求(方法、url、正文、标题)
959
960定义设置内容长度(自身、正文):
/usr/lib/python2.7/httplib.pyc in_send_请求(self、方法、url、正文、标题)
984跳过['skip_accept_encoding']=1
985
-->986 self.putrequest(方法、url、**跳过)
987
988如果正文和('content-length'不在标题名称中):
/putrequest中的usr/lib/python2.7/httplib.pyc(self、方法、url、跳过主机、跳过接受编码)
854自我状态=\u CS\u请求\u已启动
855其他:
-->856提出CannotSendRequest()
857
858#保存我们使用的方法,稍后在响应阶段需要它
无法发送请求:

有什么解决方案吗?

请确保您至少是PhantomJS 1.8,在该版本之前,他们不支持webdriver连接。我知道我的软件包管理器有一个更旧的版本,昨天我正在讨论这个问题。

PhantomJS self.driver=webdriver.PhantomJS(“C://PhantomJS.exe”)
self.driver.get(“”)

或|

  • 打开命令提示符
  • 在cmd中找到文件夹C:\Python27\Scripts并输入
    brew安装phantomjs
  • self.driver=webdriver.PhantomJS()
    self.driver.get(“”)


    我建议尝试以下方法:

    driver=webdriver.PhantomJS(可执行文件路径=r'path to executable')

    其中说明了可执行文件的路径