栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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")

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/471020.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号