硒下载的验证码图片与浏览器中的图片不同

硒下载的验证码图片与浏览器中的图片不同,第1张

下载的验证码图片与浏览器中的图片不同

因为图片的链接会在您打开该链接后

src
为您提供随机的 新验证 码图片!

src
可以从屏幕快照中截取屏幕快照,而不是从图像的上下载文件。但是,您需要下载
Pillow
pip installPillow
)并按照此答案中提到的方式使用它:

from PIL import Imagefrom selenium import webdriverdef get_captcha(driver, element, path):    # now that we have the preliminary stuff out of the way time to get that image :D    location = element.location    size = element.size    # saves screenshot of entire page    driver.save_screenshot(path)    # uses PIL library to open image in memory    image = Image.open(path)    left = location['x']    top = location['y'] + 140    right = location['x'] + size['width']    bottom = location['y'] + size['height'] + 140    image = image.crop((left, top, right, bottom))  # defines crop points    image.save(path, 'jpeg')  # saves new cropped imagedriver = webdriver.Firefox()driver.get("http://sistemas.cvm.gov.br/?fundosreg")# change framedriver.switch_to.frame("Main")# download image/captchaimg = driver.find_element_by_xpath(".//*[@id='trRandom3']/td[2]/img")get_captcha(driver, img, "captcha.jpeg")driver = webdriver.Firefox()driver.get("http://sistemas.cvm.gov.br/?fundosreg")# change framedriver.switch_to.frame("Main")# download image/captchaimg = driver.find_element_by_xpath(".//*[@id='trRandom3']/td[2]/img")get_captcha(driver, img, "captcha.jpeg")

(请注意,我对代码进行了一些更改,因此可以在您的情况下使用。)



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5674970.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存