python selenium调用ie8,不成功

新手上路,请多包涵

selenium Python调用ie8失败,已经下载了驱动程序IEDriverServer.exe。并且确定其对应环境变量设置无误,因为同时也下载了geckodriver.exe并且放置在同一目录并成功调用Firefox。

代码段如下:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
import socket

DesiredCapabilities.INTERNETEXPLORER['ignoreProtectedModeSettings']=True

browser=webdriver.Ie();
#下面都不用看了,在上面这句就卡住了


browser.get("http://x.x.x.x/")
handles=browser.window_handles
hPageHandle=browser.current_window_handle
print(hPageHandle)
for handle in handles:
    if(handle!=hPageHandle):
        browser.switch_to_window(handle)
        browser.close()
        browser.switch_to_window(hPageHandle)
        break
browser.find_element_by_id("UserID").send_keys("xxxxxx")
browser.find_element_by_id("UserID0").send_keys("xxxxxxxx")
browser.find_element_by_xpath("/html/body/div/form/table/tbody/tr[7]/td[2]/input").click()
preLen=len(browser.window_handles)
print(preLen)
browser.find_elements_by_link_text("xxxxx")[0].click()
# time.sleep(2)
oaPageHandle=""
while(preLen==len(browser.window_handles)):
    print(len(browser.window_handles))
    time.sleep(0.5)
print("end")
print(browser.window_handles)
for handle in browser.window_handles:
    if(handle!=hPageHandle):
        print("a ",handle)
        oaPageHandle=handle
        browser.switch_to_window(oaPageHandle)
        break
# time.sleep(2)

a=True
while(a == True):
    time.sleep(0.2)
    try:
        browser.find_element_by_name("userName").click()
    except:
        continue
    break
browser.find_element_by_name("userName").send_keys("xxxxxxx")
browser.find_element_by_name("passWord").send_keys("xxxxxxxxxxx")
browser.find_element_by_xpath("/html/body/form/div/div[4]/ul/input[2]").click()
print(browser.current_window_handle)
time.sleep(3)
browser.find_element_by_xpath("/html/body/div[1]/div[2]/div/div/table/tbody/tr[15]/td[6]/li/img").click()

报错信息只给出了异常名,没给出更多信息,最后跟着一大段html语句,用浏览器打开该html语句如下 :
图片描述

报错信息如下:

Traceback (most recent call last):
  File "C:/Users/nnceb/PycharmProjects/seleniumer/seleniumer.py", line 10, in <module>
    browser=webdriver.Ie();
  File "C:\Python34\lib\site-packages\selenium\webdriver\ie\webdriver.py", line 82, in __init__
    desired_capabilities=capabilities)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 151, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 240, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 165, in check_response
    raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta http-equiv="Content-Type" CONTENT="text/html; charset=utf-8">
<title>ERROR: The requested URL could not be retrieved</title>
<style type="text/css"><!-- 
 body
{
        font-family: "Microsoft YaHei", "宋体", "Segoe UI", "verdana", "arial";
        background-image: url(http://www.netentsec.com/download/nps/squid_bg.jpg);
        background-repeat: repeat-x;
        background-color: #E8EAEF;
        margin-top: 20px;
        margin-left: 20px;
        color: #575757;
}
a
{
        color: rgb(19,112,171); font-size: 1em;
        font-weight: normal;
        text-decoration: none;
        margin-left: 0px;
        vertical-align: top;
}
a:link, a:visited
{
        color: rgb(19,112,171);
        text-decoration: none;
        vertical-align: top;
}
a:hover
{
        color: rgb(7,74,229);
        text-decoration: underline;
}
p
{
        font-size: 0.9em;
}    
h1 /* used for Title */
{
        color: #4465A2;
        font-size: 1.1em;
        font-weight: normal;
        vertical-align:bottom;
        margin-top: 7px;
        margin-bottom: 4px;
}
h2 /* used for Heading in Main Body */
{
        font-size: 0.9em;
        font-weight: normal;
        margin-top: 20px;       
        margin-bottom: 1px;
}
h3 /* used for text in main body */
{
        font-size: 0.9em;
        font-weight: normal;
        margin-top: 10px;       
        margin-bottom: 1px;
}
h4 /* used for task links*/
{
        font-size: 0.9em;
        font-weight: normal;
        margin-top: 12px;
        margin-bottom: 1px;
}
h5 /* used for Heading in InfoBlock */
{
        font-size: 1em;
        font-weight: normal;
        margin-bottom: 0px;
        margin-top: 1px;        
        font-size: 0.9em;
        color: #575757;
}
.actionIcon /* used for task link icons */
{
        vertical-align: middle;
        margin-top: 0px;
        margin-right: 6px;
}
.infoBlock
{
        padding-left: 25px;
        font-size: 0.9em;
        display: block;
        line-height: 1.1 em;
        color: #575757;
}
.errorCodeAndDivider
{
        font-size: 0.7em;
        font-weight: normal;
        color: #787878;
}
ul, ol
{
        font-size: 0.9em;
        list-style-position: outside;
        margin-top: 1px;
        margin-bottom: 1px;
        padding-top: 1px;
        padding-bottom: 1px;
        line-height: 1.3em;
}
.myul li{ list-style-type:none;}
.myul li{ background:url(http://www.netentsec.com/download/nps/s.gif) no-repeat 4px 8px; line-height:2.5em; padding-left:30px; font-family:"Microsoft YaHei", "宋体"; font-size: 0.9em;}
.divider
{
        border-bottom: #B6BCC6 1px solid;
}


body
:lang(fa) { direction: rtl; font-size: 100%; font-family: Tahoma, Roya, sans-serif; float: right; }
:lang(he) { direction: rtl; }
 --></style>
</head><body id=ERR_CONNECT_FAIL>
<div id="titles">
<h1>ERROR</h1>
<h2>The requested URL could not be retrieved</h2>
</div>
<hr>

<div id="content">
<p>The following error was encountered while trying to retrieve the URL: <a href="http://127.0.0.1:5555/session">http://127.0.0.1:5555/session</a></p>

<blockquote id="error">
<p><b>Connection to 127.0.0.1 failed.</b></p>
</blockquote>

<p id="sysmsg">The system returned: <i>(111) Connection refused</i></p>

<p>The remote host or network may be down. Please try the request again.</p>

<p>Your cache administrator is <a href="mailto:webmaster?subject=CacheErrorInfo%20-%20ERR_CONNECT_FAIL&amp;body=CacheHost%3A%20netentsec-nps-192.168.168.1%0D%0AErrPage%3A%20ERR_CONNECT_FAIL%0D%0AErr%3A%20(111)%20Connection%20refused%0D%0ATimeStamp%3A%20Fri,%2017%20Nov%202017%2009%3A37%3A17%20GMT%0D%0A%0D%0AClientIP%3A%2012.35.17.241%0D%0AServerIP%3A%20127.0.0.1%0D%0A%0D%0AHTTP%20Request%3A%0D%0APOST%20%2Fsession%20HTTP%2F1.1%0AAccept-Encoding%3A%20identity%0D%0AHost%3A%20127.0.0.1%3A5555%0D%0AAccept%3A%20application%2Fjson%0D%0AConnection%3A%20close%0D%0AContent-Type%3A%20application%2Fjson%3Bcharset%3DUTF-8%0D%0AContent-Length%3A%2084%0D%0AUser-Agent%3A%20Python%20http%20auth%0D%0A%0D%0A%0D%0A">webmaster</a>.</p>

<br>
</div>

<hr>
<div id="footer">
<p>Generated Fri, 17 Nov 2017 09:37:17 GMT by netentsec-nps-192.168.168.1 (nps/2.3.1)</p>
<!-- ERR_CONNECT_FAIL -->
</div>
</body></html>
Process finished with exit code 1

一些自己的猜测,根据那段html代码,我在想是不是selenium启动后没有连接上IEDriverServer,于是我手动打开IEDriverServer,发现他总是占用5555端口(末尾有图),就去看了源码发现selenium内部是随机选用一个可用端口的,于是手动指定使用5555端口并且运行代码前打开IEDriverServer但是仍然不行,报错信息一模一样

图片描述

阅读 5.3k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏