无法使用 IE11 和 Selenium 使用 Python 检索 page_source

Unable to retrieve page_source using IE11 and Selenium using Python

driver = webdriver.Ie()
driver.implicitly_wait(5)
url = "http://nontax.hebcz.cn"
driver.get(url)
driver.maximize_window()
print("=======")
print(driver.page_source)
print("=======")

这是我的代码,它什么都不打印

硒 2.53.1

我添加了 Reg 并更改了 IE 的安全选项

我能做什么???

如果您看到任何错误,您没有提到。但是,使用 v3.141.0 我可以按照下面的解决方案提取 page_source

代码块:

from selenium import webdriver

driver = webdriver.Ie(executable_path=r'C:\WebDrivers\IEDriverServer.exe')
driver.get('http://nontax.hebcz.cn/')
print("Page title is: %s" %(driver.title))
print("=======")
print(driver.page_source)
print("=======")
driver.quit()

控制台输出:

Page title is: 河北省非税云缴费平台-
=======
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">

<meta name="viewport" content="width=1200">
<meta name="_csrf" content="711baa60-38b0-4328-8704-f404f3d08c49">
<title>河北省非税云缴费平台-</title>
<script src="/static/js/jquery-1.9.1.min.js"></script>
<link href="/static/images/favicon.ico" rel="shortcut icon" type="image/x-icon">
<link href="/static/css/style.css" rel="stylesheet" type="text/css">
<script src="/static/js/zzsc.js" type="text/javascript"></script>
<script type="text/javascript">var footer_Positon =0;$(function () {var token =$("meta[name='_csrf']").attr("content");$(document).ajaxSend(function(e,xhr,options) {xhr.setRequestHeader("CSRFToken",token);});});$(document).ready(function(){var usernav =document.getElementById('usernav');var u =document.getElementById('u');u.onmouseover=function(){usernav.style.display='block'
}
u.onmouseout=function(){usernav.style.display='none'
}
})
</script>
<script type="text/javascript">function unUsed() {alert("缴费大厅暂未开放,请耐心等候!");}
function unInvoice() {alert("国庆间暂停访问!");}
</script>
<style>.footer{height:99px;width:100%;position:fixed;bottom:0;}</style>
</head>
<body>
<div style="min-width: 1200px;">
<div class="top_head">
<div class="topnav">
<ul>
<li><a href="/static/getIndex">首页 </a></li>
<li id="u">
<a href="#">用户中心 <span class="pot"></span></a>
<div class="user_list" id="usernav">
<span class="list_cur"><a href="/web/main/view?path=web/user/userInfo">个人信息</a></span>
<span><a href="/web/main/view?path=web/paycenter/payorderlist">缴费列表</a></span>
<span><a href="/web/main/view?path=web/paycenter/pay">常用缴费</a></span>
<span><a href="/web/main/view?path=web/paycenter/electronic-bill">电子票据</a></span>
</div>
</li>
<li><a href="/unit/main/view?path=unit/index">电子票据</a></li>
<li><a href="/bill/billCheck">票据查验</a></li>
<li><a href="/static/getInfoList?type=notice">工作动态</a></li>
<li><a href="/static/getInfoList?type=news">政策规定</a></li>
<li><a href="/static/getInfoList?type=laws">办事指南</a></li>
</ul>

</body></html>
=======

您尝试过使用 Desired Capabilities 吗?

您可以尝试在 Kiosk 模式下启动 IE,如文档中所述:

-k : Starts Internet Explorer in kiosk mode. The browser opens in a maximized window that does not display the address bar, the navigation buttons, or the status bar.

documentation 之后,您可以通过在启动驱动程序之前添加选项来实现您的代码:


from selenium import webdriver

options = webdriver.IeOptions() options.add_argument('-k') 
options.force_create_process_api = True 
driver = webdriver.Ie(options=options)

driver.implicitly_wait(5)
url = "http://nontax.hebcz.cn"
driver.get(url)
print("=======")
print(driver.page_source)
print("=======")

我建议尝试从 here 下载最新版本的 IEDriverServer。

我试过测试你的示例代码,它工作正常。