Download image with selenium python

0
0

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

Is there any solution to get picture from browser (like ‘save picture as’ button)?

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

  1. get screenshot of the browser
  2. get position of picture
  3. crop captcha from screenshot using opencv

link of the dynamic capcha – link

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

  • You must to post comments
0
0

Here’s a complete example (using google’s recaptcha as a target):

import urllib
from selenium import webdriver
driver
= webdriver.Firefox()
driver
.get('http://www.google.com/recaptcha/demo/recaptcha')
# 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 dynamic generated images is that there is 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://moscowsg.megafon.ru/ps/scc/php/cryptographp.php?PHPSESSID=mfc540jkbeme81qjvh5t0v0bnjdr7oc6&ref=114&w=150')
    driver
    .save_screenshot("screenshot.png")
    driver
    .close()
  • simulate right click + “Save As”. See this thread for more info.

Hope that helps.

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website