Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

I want to get a captcha image from the browser. I have got a URL of this picture, but this picture changes each updated time (URL is constant).

Is there any solution to get a picture from the browser (like 'save picture as' button)?

From the other hand, I think it should be work:

  1. get screenshot of the browser
  2. get the position of the picture
  3. crop captcha from screenshot using OpenCV link of the dynamic captcha 

The problem was solved via screenshot:


img = browser.find_element_by_xpath('//*[@id="cryptogram"]')

loc = img.location

image = cv.LoadImage('screenshot.png', True)

out = cv.CreateImage((150,60), image.depth, 3)

cv.SetImageROI(image, (loc['x'],loc['y'],150,60))

cv.Resize(image, out)

cv.SaveImage('out.jpg', out)

Thanks in advance!

1 Answer

0 votes
by (62.9k points)

Here's a complete example (using google's Recaptcha as a target):

import urllib

from selenium import webdriver

 driver = webdriver.Firefox()


 # get the image source

img = driver.find_element_by_xpath('//div[@id="recaptcha_image"]/img')

src = img.get_attribute('src')

 # download the image

urllib.urlretrieve(src, "captcha.png")



The problem with the dynamically generated image is that there will always be a new image generated each time you request it. In that case, you have several options:

Take a screenshot

from selenium import webdriver

driver = webdriver.Firefox()




to simulate right-click + "Save As". 

I hope this helps!

If you wish to Learn Selenium visit this Selenium Training by Intellipaat.

For more information, kindly refer to our Python Certification course.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.5k answers


108k users

Browse Categories