我想通过硒获得验证码
我使用此代码可以很好地工作:
import base64
from selenium import webdriver
driver = webdriver.Firefox()
driver.set_script_timeout(10)
driver.get("http://sistemas.cvm.gov.br/?fundosreg")
driver.switch_to.frame("Main")
# find the captcha element
ele_captcha = driver.find_element_by_xpath("//img[contains(./@src, 'RandomTxt.aspx')]")
# get the captcha as a base64 string
img_captcha_base64 = driver.execute_async_script("""
var ele = arguments[0], callback = arguments[1];
ele.addEventListener('load', function fn(){
ele.removeEventListener('load', fn, false);
var cnv = document.createElement('canvas');
cnv.width = this.width; cnv.height = this.height;
cnv.getContext('2d').drawImage(this, 0, 0);
callback(cnv.toDataURL('image/jpeg').substring(22));
}, false);
ele.dispatchEvent(new Event('load'));
""", ele_captcha)
# save the captcha to a file
with open(r"captcha.jpg", 'wb') as f:
f.write(base64.b64decode(img_captcha_base64))
来自Selenium downloading different captcha image than the one in browser的来源
但在此代码中不起作用
<div style="display: flex; align-items: center;">
<div style="cursor: pointer; height: 40px; line-height: 40px; width: 100%; border: 1px solid rgb(217, 217, 217);
border-radius: 4px; background-image: url("https://web.com/getimg?ran=8472926");
background-size: 100% 100%;"></div>
</div>
因为没有元素id或src img或其他任何内容。 我尝试其他代码,但无法下载验证码img。 谢谢你。