IE和Chrome无法使用Selenium2 Python

时间:2013-07-12 22:35:21

标签: python internet-explorer google-chrome firefox selenium-webdriver

我似乎无法通过Selenium 2的Python库打开Goog​​le Chrome或Internet Explorer。我使用的是Windows 7,64位。

我已完成以下步骤:

  • 已安装的python - 2.7.5
  • 已安装硒2.33
  • 包含C:\ Python27& C:\ Python27 \ Scripts in Environment Variable - Path
  • 下载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()

OR

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启动的,但窗口永远不会出现。

2 个答案:

答案 0 :(得分:7)

在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: Disable http_proxy in urllib2中所述禁用它时,您可以解决问题:

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")

答案 1 :(得分:1)

我无法用我下载的路径解决这个问题,但是已经能够通过定义驱动程序的路径来解决这个问题,如下所示:

   driver = webdriver.Chrome('C:\path\to\chromedriver')

   driver = webdriver.Ie('C:\path\to\iedriver')
相关问题