python selenium iewebdriver

时间:2017-01-20 08:44:30

标签: python selenium-webdriver

我正在尝试使用pythonselenium在使用IEDriverServer.exe且在保护模式关闭的情况下在Internet Explorer中运行某些测试。当我尝试使用Google Chrome驱动程序时,它会按预期运行。

CODE

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
iedriver = "C:\Program Files\Internet Explorer\IEDriverServer.exe"
os.environ["webdriver.ie.driver"] = iedriver
driver.get("http://www.baidu.com")

错误

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    driver.get("http://www.baidu.com")
NameError: name 'driver' is not defined
>>> driver = webdriver.Ie(iedriver)

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    driver = webdriver.Ie(iedriver)
  File "F:\Python27\lib\site-packages\selenium\webdriver\ie\webdriver.py", line 57, in __init__
    desired_capabilities=capabilities)
  File "F:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "F:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
    response = self.execute(Command.NEW_SESSION, capabilities)
  File "F:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 234, in execute
    response = self.command_executor.execute(driver_command, params)
  File "F:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 408, in execute
    return self._request(command_info[0], url, body=data)
  File "F:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 478, in _request
    resp = opener.open(request, timeout=self._timeout)
  File "F:\Python27\lib\urllib2.py", line 429, in open
    response = self._open(req, data)
  File "F:\Python27\lib\urllib2.py", line 447, in _open
    '_open', req)
  File "F:\Python27\lib\urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "F:\Python27\lib\urllib2.py", line 1228, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "F:\Python27\lib\urllib2.py", line 1201, in do_open
    r = h.getresponse(buffering=True)
  File "F:\Python27\lib\httplib.py", line 1136, in getresponse
    response.begin()
  File "F:\Python27\lib\httplib.py", line 453, in begin
    version, status, reason = self._read_status()
  File "F:\Python27\lib\httplib.py", line 417, in _read_status
    raise BadStatusLine(line)
BadStatusLine: ''

2 个答案:

答案 0 :(得分:0)

我建议尝试使用chrome或firefox,但如果您在IE上设置,我认为其中一个问题是驱动程序未定义。你改为定义iedriver,

iedriver = "C:\Program Files\Internet Explorer\IEDriverServer.exe"
driver.get("http://www.baidu.com")

将其更改为,

iedriver = "C:\Program Files\Internet Explorer\IEDriverServer.exe"
iedriver.get("http://www.baidu.com")

或者

driver = "C:\Program Files\Internet Explorer\IEDriverServer.exe"
driver.get("http://www.baidu.com")

答案 1 :(得分:0)

要在python中创建新的IE WebDriver,你可以

iedriver = webdriver.Ie("C:\Program Files\Internet Explorer\IEDriverServer.exe")

this question获取答案。

相关问题