因为图片的链接会在您打开该链接后
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")
(请注意,我对代码进行了一些更改,因此可以在您的情况下使用。)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)