无法在Selenium中找到元素

时间:2013-09-12 13:46:52

标签: python firefox selenium

我正在尝试下载每次加载页面时其URL和内容动态更改的验证码图像,我知道我可以为浏览器截取屏幕截图并找到验证码图像位置,我无法找到验证码img。

从我找到的HTML源代码

//此脚本用于生成验证码

<iframe marginheight="0" marginwidth="0" scrolling="no" frameborder="0" width="203" height="53" name="Captcha" src="/efs/servlet/efs/jsp-ns/captcha.jsp"></iframe>

//当我点击src =“/ efs / servlet / efs / jsp-ns / captcha.jsp”时,它引导我进入这个

<html>
<head><meta scheme='a1afcc517bec909bf5c3fddea7c83c3d' name='TSd58639' content='b133d7457db43c81' /> <meta scheme='eb1e31097f37b3d64bef23cbd5cab231' name='1000' content='5' /><!-- 9cc5da25f89a21d1fbb5ffa18da0bb73 --><script type="text/javascript">//<![CDATA[
eval(function(a){var f=a.split("");var c=f.length;var b=parseInt(f[0]+f[1],16);var e=String.fromCharCode(b);for(var d=2;d<c;d++){var g=(parseInt(f[d]+f[d+1],16)-b)%256;b=g;e+=String.fromCharCode(g);d++}return e}("288..."));
</script>
<script language="JavaScript">var pn = "CSRT"; var pv = '3642466061891909727';
eval(function(a){var f=a.split("");var d=f.length;var c=parseInt(f[0]+f[1],16);var e=String.fromCharCode(c);for(var b=2;b<d;b++){var g=(parseInt(f[b]+f[b+1],16)-c)%256;c=g;e+=String.fromCharCode(g);b++}return e}("288edbe3..."));
</script>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<img src="Captcha.jpg?t=1378993130057" border=1/>
</body>
</html>

此行'<img src="Captcha.jpg?t=1378993130057" border=1/>'定义验证码网址但数字't = 1378993130057'动态更改

我见过这个帖子 Download image with selenium python 但我不明白作者如何找到图像位置,如

img = browser.find_element_by_xpath('//*[@id="cryptogram"]')

for google captcha [http://www.google.com/recaptcha/demo/recaptcha]

img = driver.find_element_by_xpath('//div[@id="recaptcha_image"]/img')

python 2.6 我正在使用Selenuim浏览网站

更新

try:
    browser.save_screenshot('screenshot.png')
    img = browser.find_element_by_xpath('//body/img')
    src = img.get_attribute('src')
    loc = img.location

except Exception,e:
    print e

输出

Message: u'Unable to locate element: {"method":"xpath","selector":"//body/img"}' ; Stacktrace: 
    at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmppjlmPW/extensions/fxdriver@googlecode.com/components/driver_component.js:8899)
    at FirefoxDriver.prototype.findElement (file:///tmp/tmppjlmPW/extensions/fxdriver@googlecode.com/components/driver_component.js:8908)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmppjlmPW/extensions/fxdriver@googlecode.com/components/command_processor.js:10840)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmppjlmPW/extensions/fxdriver@googlecode.com/components/command_processor.js:10845)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmppjlmPW/extensions/fxdriver@googlecode.com/components/command_processor.js:10787)

更新#2

from selenium import webdriver
import datetime
from selenium.webdriver.common.proxy import *


print '[+] Starts at '+ datetime.datetime.now().isoformat()

browser = webdriver.Firefox() 
browser.get("https://www.example.com") 


try:
    browser.save_screenshot('screenshot.png')
    img = browser.find_element_by_xpath('//body/img')
    src = img.get_attribute('src')
    loc = img.location

except Exception,e:
    print e


browser.delete_all_cookies()
browser.close()

print '[+] Done at ' + datetime.datetime.now().isoformat()

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您可以通过xpath获取img标记,获取src属性值,然后通过urlretrieve下载:

import urllib

img = browser.find_element_by_xpath('//body/img')
src = img.get_attribute("src")
urllib.urlretrieve(src, "captcha.png")

答案 1 :(得分:0)

import urllib

img = browser.find_element_by_cssselector("img[src*='Captcha.jpg']")

src = img.get_attribute("src")

urllib.urlretrieve(src, "Captcha.jpg")

尝试此方法,让我知道它是否有效。