IE和Chrome无法与Selenium2 Python一起使用
问题内容:
我似乎无法通过Selenium 2的Python库打开Google Chrome或Internet Explorer。我正在使用Windows
7、64位。
我已经完成以下步骤:
- 安装的python-2.7.5
- 已安装硒2.33
- 环境变量-路径中包含的C:\ Python27和C:\ Python27 \ Scripts
- 下载了支持v27-30(我在28岁)的32位(我正在运行64位,但找不到32位版本)Windows Chrome驱动程序,并将其放入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启动的,但是窗口永远不会显示。
问题答案:
在python-seleniumwebdriver.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")