IE and Chrome not working with Selenium2 Python

2019-02-18 07:31发布

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.

2条回答
再贱就再见
2楼-- · 2019-02-18 08:02

I haven't been able to solve this problem with the path I've downloaded it to, but have been able to workaround it by defining the path to the driver, like so:

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

or

   driver = webdriver.Ie('C:\path\to\iedriver')
查看更多
3楼-- · 2019-02-18 08:03

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")
查看更多
登录 后发表回答