Ker*_*san 1 html python selenium google-chrome
我想使用Python使用Selenium 登录此页面.但是浏览器中显示的页面与HTML中描述的页面不同.Firefox或Chrome webdriver获得相同的结果.
chromedriver = "./chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
# OR
#driver = webdriver.Firefox()
driver.get('http://www.anb.org/login.htmlurl=%2Farticles%2Fhome.html&ip=94.112.189.79&nocookie=0')
# get screenshot of page
driver.get_screenshot_as_file('./01.png')
#get source code of page
print driver.page_source
Run Code Online (Sandbox Code Playgroud)
我不允许发布图像,但图像与Web浏览器中显示的页面完全相同.
驱动程序的HTML代码:
<html><head>
<title>American National Biography Online</title>
<script>
document.write ("<FRAMESET ROWS=\"103,*\" FRAMEBORDER=0 BORDER=0 FRAMESPACING=0>\n");
document.write (" <FRAME SRC=\"top-home.html\" MARGINWIDTH=0 MARGINHEIGHT=0 SCROLLING=NO>\n");
if (location.search) {
var url = unescape (location.search);
url = (new String(url)).substring(1);
if (url.indexOf ("&") == -1) {
document.write (" <FRAME SRC=\"" + url + "\" MARGINWIDTH=0 MARGINHEIGHT=0>\n");
} else {
document.write (" <FRAME SRC=\"main-home.html" + location.search + "\" MARGINWIDTH=0 MARGINHEIGHT=0>\n");
}
}
else
document.write (" <FRAME SRC=\"main-home.html\" NAME=atop MARGINWIDTH=0 MARGINHEIGHT=0>\n");
document.write ("</FRAMESET>\n");
</script></head>
<frameset rows="103,*" frameborder="0" border="0" framespacing="0">
<frame src="top-home.html" marginwidth="0" marginheight="0" scrolling="NO">
<frame src="main-home.html?url=%2Farticles%2Fbrowse.html&ip=94.112.189.79&nocookie=0" marginwidth="0" marginheight="0">
</frameset>
<noframes>
</noframes>
</html>
Run Code Online (Sandbox Code Playgroud)
如您所见,HTML和图片不匹配.
也许问题是框架?
我的配置:
osx 10.8.5
python 2.7.5
chrome browser 28.0.1500.71
firefox browser 24.0
Run Code Online (Sandbox Code Playgroud)
我安装了最新的chrome/firefox webdrivers,但我真的不知道如何找到版本.
该属性page_source几乎无用:它返回服务器发送到浏览器的第一个HTML版本; 它不是当前 DOM 的副本.
获取副本的最佳方法是使用JavaScript和innerHTML:
js_code = "return document.getElementsByTagName('html').innerHTML"
your_elements = sel.execute_script(js_code)
Run Code Online (Sandbox Code Playgroud)
另请注意,innerHTML不跨越frame元素.由于代码中有框架,因此需要单独检查这些框架:
frames = driver.find_element_by_tag_name("frame")
js_code = "return arguments[0].innerHTML"
your_elements = sel.execute_script(js_code, frames[0])
Run Code Online (Sandbox Code Playgroud)
你也可以切换到一个框架:
driver.switch_to_frame("frameName")
Run Code Online (Sandbox Code Playgroud)
之后,所有代码都将在此框架的上下文中执行.别忘了换回来.
| 归档时间: |
|
| 查看次数: |
2286 次 |
| 最近记录: |