您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

因为图片链接会在您打开该链接src为您提供一个随机的 码图片

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

from PIL import Image
from selenium import webdriver

def 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 image


driver = webdriver.Firefox()
driver.get("http://sistemas.cvm.gov.br/?fundosreg")

# change frame
driver.switch_to.frame("Main")

# download image/captcha
img = 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 frame
driver.switch_to.frame("Main")

# download image/captcha
img = driver.find_element_by_xpath(".//*[@id='trRandom3']/td[2]/img")
get_captcha(driver, img, "captcha.jpeg")

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

其他 2022/1/1 18:16:25 有489人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶