首页 \ 问答 \ IE和Chrome无法使用Selenium2 Python(IE and Chrome not working with Selenium2 Python)

IE和Chrome无法使用Selenium2 Python(IE and Chrome not working with Selenium2 Python)

我似乎无法通过Selenium 2的Python库打开谷歌浏览器或Internet Explorer。 我使用的是Windows 7,64位。

我已完成以下步骤:

  • 已安装的python - 2.7.5
  • 安装硒2.33
  • 在环境变量 - 路径中包含C:\ Python27&C:\ Python27 \ Scripts
  • 下载32位(我正在运行64位但我找不到32位版本)windows Chrome驱动程序支持v27-30(我28岁)并将其放入C:\ Python27 \ Scripts
  • 下载支持IE9的64位IE驱动程序(我将IE10降级为IE9)。 我将驱动程序放入C:\ Python27 \ Scripts

每当我打字:

from selenium import webdriver
driver = webdriver.Ie()

要么

from selenium import webdriver
driver = webdriver.Chrome()

进入Python shell,没有浏览器弹出,shell只会冻结几分钟然后输出错误信息。

IE错误消息:

selenium.common.exceptions.WebDriverException: Message: 'Can not connect to the IEDriver'

Chrome错误消息:

urllib2.HTTPError: HTTP Error 503: Service Unavailable

它与firefox完美搭配。 有趣的是,该过程(IEDriver和ChromeDriver)是根据TaskManager启动的,但窗口永远不会出现。


I can't seem to open up Google Chrome or Internet Explorer through Selenium 2's Python library. I am using Windows 7, 64 bit.

I have completed the following steps:

  • Installed python - 2.7.5
  • Installed selenium 2.33
  • Included C:\Python27 & C:\Python27\Scripts in the Environment Variable - Path
  • Downloaded the 32 bit (I am running 64 bit but I could not find the 32 bit version) windows Chrome Driver that supports v27-30 (I am on 28) and placed it into C:\Python27\Scripts
  • Downloaded the 64 bit IE driver that supports up to IE9 (I downgraded IE10 to IE9). I placed the driver into C:\Python27\Scripts

Whenever I type:

from selenium import webdriver
driver = webdriver.Ie()

OR

from selenium import webdriver
driver = webdriver.Chrome()

into the Python shell, no browser pops up, the shell just freezes for a couple of minutes and then outputs an error message.

IE Error message:

selenium.common.exceptions.WebDriverException: Message: 'Can not connect to the IEDriver'

Chrome Error message:

urllib2.HTTPError: HTTP Error 503: Service Unavailable

It works perfectly fine with firefox. The funny thing is, is that the process (IEDriver and ChromeDriver) starts per the TaskManager, but the window never shows up.

更新时间:2022-10-20 13:10

最满意答案

在python-selenium webdriver.Ie只是执行IEDriver.exe并通过webdriver.Remote连接到它的快捷方式。 例如,您可以从命令行启动IEDriver.exe

> IEDriverServer.exe
Started InternetExplorerDriver server (64-bit)
2.39.0.0
Listening on port 5555

并使用以下代码替换webdriver.Ie()

webdriver.Remote(command_executor='http://127.0.0.1:5555',
                 desired_capabilities=DesiredCapabilities.INTERNETEXPLORER)`

你会得到相同的结果。

特别是在您的情况下,您很可能有一些系统代理设置强制它通过代理服务器连接到127.0.0.1 。 可能当您按照回答Python中的描述禁用它时:在urllib2中禁用http_proxy ,您可以解决问题:

import selenium
import urllib2
from contextlib import contextmanager

@contextmanager
def no_proxies():
    orig_getproxies = urllib2.getproxies
    urllib2.getproxies = lambda: {}
    yield
    urllib2.getproxies = orig_getproxies

with no_proxies():
    driver = selenium.webdriver.Ie()
    driver.get("http://google.com")

In python-selenium webdriver.Ie is just a shortcut for executing IEDriver.exe and connecting to it through webdriver.Remote. For example, you could start IEDriver.exe from command line:

> IEDriverServer.exe
Started InternetExplorerDriver server (64-bit)
2.39.0.0
Listening on port 5555

and replace webdriver.Ie() with the following code:

webdriver.Remote(command_executor='http://127.0.0.1:5555',
                 desired_capabilities=DesiredCapabilities.INTERNETEXPLORER)`

You will get the same result.

Specifically in your case is most likely that you have some system proxy settings that force it connect to 127.0.0.1 through proxy server. Probably when you disable it as described in answer Python: Disable http_proxy in urllib2, you can solve the problem:

import selenium
import urllib2
from contextlib import contextmanager

@contextmanager
def no_proxies():
    orig_getproxies = urllib2.getproxies
    urllib2.getproxies = lambda: {}
    yield
    urllib2.getproxies = orig_getproxies

with no_proxies():
    driver = selenium.webdriver.Ie()
    driver.get("http://google.com")

相关问答

更多

相关文章

更多

最新问答

更多
  • 在javascript中创建类以创建对象并在Java中创建类和对象之间的区别(Difference between creating a class in javascript to create an object and creating an class and object in Java)
  • Facebook API:将身份验证详细信息从Javascript SDK发送到PHP SDK(Facebook API: Send authentication detail from Javascript SDK to PHP SDK)
  • 如何停止队列动画jquery?(How can I stop queue animation jquery?)
  • 使用C#的井字游戏中的人工智能(Artificial Intelligence in Tic-Tac-Toe using C#)
  • 多少流量可以共享虚拟主机(对于Python Django站点)支持?(How Much Traffic Can Shared Web Hosting (for a Python Django site) support?)
  • 带有CIFilters的CAShapeLayer(CAShapeLayer with CIFilters)
  • 如何在Angular 2中读取JSON #text(How to read in Angular 2 the JSON #text)
  • 如何在xml中读取自闭标签的属性?(How to read self closing tag's attribute in xml?)
  • 无法使用http put将图像上传到亚马逊S3(Cannot upload image to amazon s3 using http put)
  • 文件结束无限循环(end of file infinite while-loop)
  • 在cpp的模板(template in cpp)
  • 在构建库时,clang和clang ++有什么区别?(What's the difference between clang and clang++ when building a library?)
  • ng类中的表达式(expression inside ng-class)
  • 在PHP中获取随机布尔值true / false(Get random boolean true/false in PHP)
  • 管道的高效分块用于严格的字节串(Efficient chunking of conduit for strict bytestring)
  • Python ternary_operator(如果其他标志做了其他操作,则执行其他操作)(Python ternary_operator (do-somthing if flag else do-another))
  • Sencha Touch面具发布(Sencha Touch mask ondisclosure)
  • 验证脚本上的通知[重复](Notices on validation script [duplicate])
  • 朋友功能(friend function)
  • 基于角坐标平移和变换平面几何(Translate and transform plane geometry based on corner coordinates)
  • Rails:'如果在本地运行'条件javascript标记包括(Rails: 'if running locally' conditional javascript tag include)
  • 解压文件(Unzipping files)
  • 使用ui-router以角度加载变量状态(loading in variable states with ui-router in angular)
  • 创建Azure云服务需要多长时间?(how long does it take to create an Azure Cloud Service? How to view log information?)
  • 指向整数的指针数组(Array of pointers to integers)
  • Laravel服务提供商没有看到我的包的主要类(Laravel service provider does not see the main class of my package)
  • 这个关于VSS / RSS / PSS / USS的解释是否准确?(Is this explanation about VSS/RSS/PSS/USS accurate?)
  • 在Django-Admin中通过row-id排序显示项目(Ordering the display items by row-id in Django-Admin)
  • 如何使用cythonize启用`--embed`?(How to enable `--embed` with cythonize?)
  • 用于将文本多行设置的Excel脚本(Excel script for ereasing text multiple rows)