0 votes
1 view
in DevOps and Agile by (19.8k 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:

browser.save_screenshot('screenshot.png')

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 (63.4k points)

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

import urllib

from selenium import webdriver

 

driver = webdriver.Firefox()

driver.get('https://intellipaat.com/')

 

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

driver.close()

UPDATE:

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

driver.get('https://intellipaat.com/')

driver.save_screenshot("screenshot.png")

driver.close()

to simulate right-click + "Save As". 

I hope this helps!

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

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


Categories

...