Selenium uploads a different captcha image than the image in the browser

I'm trying to upload an image with images using Selenium, however the image being uploaded is different from the image shown in the browser. If I try to download the image again without changing the browser, I will get another.

Any thoughts?

from selenium import webdriver import urllib 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") src = img.get_attribute('src') urllib.request.urlretrieve(src, "captcha.jpeg") 
+5
source share
3 answers

Since the src image link gives you a random new captcha image when you open this link!

Instead of downloading a file from an src image, you can take a screenshot to get it in a browser. However, you need to download Pillow ( pip install Pillow ) and use it as indicated in this answer :

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

(Note that I modified the code a bit so that it can work in your case.)

+12
source

You can get a rendered image of Canadan as part of Javascript. This is faster than capturing and cropping a screenshot:

 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)) 
+9
source

If you already have an image loaded, instead of execute_async_script, go with

 import base64 img_base64 = browser.execute_script(""" var ele = arguments[0]; var cnv = document.createElement('canvas'); cnv.width = ele.width; cnv.height = ele.height; cnv.getContext('2d').drawImage(ele, 0, 0); return cnv.toDataURL('image/jpeg').substring(22); """, browser.find_element_by_xpath("//your_xpath")) with open(r"image.jpg", 'wb') as f: f.write(base64.b64decode(img_base64)) 
+1
source

Source: https://habr.com/ru/post/1247163/


All Articles