IE和Chrome不能使用Selenium2 Python [英] IE and Chrome not working with Selenium2 Python

查看:202
本文介绍了IE和Chrome不能使用Selenium2 Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我已完成以下步骤:
$ b $ ul

  • 安装了python - 2.7.5

  • 安装了selenium 2.33

  • 包括C:\ Python27& C:\ Python27 \环境变量中的脚本变量 - 路径
  • 下载32位(我正在运行64位,但找不到32位版本) v27-30(我在28),并将其放置到C:\Python27\Scripts

  • 下载支持IE9的64位IE驱动程序(我将IE10降级到IE9) 。我将驱动程序放入C:\Python27\Scripts



  • 每当我键入:

     来自selenium import webdriver 
    driver = webdriver.Ie()



    p

      from selenium import webdriver 
    driver = webdriver.Chrome()

    放到Python shell中,没有浏览器弹出,shell冻结了几分钟,然后输出错误信息。

    IE错误信息:

      selenium.common.exceptions.WebDriverException :消息:'无法连接到IEDriver'

    Chrome错误消息:

      urllib2.HTTPError:HTTP错误503:服务不可用

    使用firefox可以很好地工作。有趣的是,这个过程(IEDriver和ChromeDriver)是通过TaskManager启动的,但是这个窗口永远不会显示出来。 解决方案

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

     > IEDriverServer.exe 
    启动InternetExplorerDriver服务器(64位)
    2.39.0.0
    监听端口5555

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

    <$ p $ ($ command_executor ='http://127.0.0.1:5555',
    desired_capabilities = DesiredCapabilities.INTERNETEXPLORER)`
    webdriver.Remote pre>

    您将得到相同的结果。



    具体而言,您的情况最有可能是您有一些系统代理设置强制它通过代理服务器连接到 127.0.0.1 。可能当您按照 Python中的描述禁用它:Python:在urllib2中禁用http_proxy 可以解决这个问题:

    pre $ import $ $ $ b $ import b


    $ b $

    @contextmanager
    def no_proxies():
    orig_getproxies = urllib2.getproxies
    urllib2.getproxies = lambda:{}
    yield
    urllib2 .getproxies = orig_getproxies
    $ b $ with no_proxies():
    driver = selenium.webdriver.Ie()
    driver.get(http://google.com)


    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.

    解决方案

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

    这篇关于IE和Chrome不能使用Selenium2 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆