Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download image with selenium python

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

like image 677
user1941407 Avatar asked Jun 28 '13 09:06

user1941407


People also ask

How do I save an image using Python Selenium?

We can download images with Selenium webdriver in Python. First of all, we shall identify the image that we want to download with the help of the locators like id, class, xpath, and so on. We shall use the open method for opening the file in write and binary mode (is represented by wb).


2 Answers

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.

like image 60
alecxe Avatar answered Sep 24 '22 11:09

alecxe


It's ok to save a screenshot from the whole page and then cut the image from, but you can also to use the "find" method from "webdriver" to locate the image you want to save, and write the "screenshot_as_png" property like below:

from selenium import webdriver driver = webdriver.Firefox() driver.get('https://www.webpagetest.org/') with open('filename.png', 'wb') as file:     file.write(driver.find_element_by_xpath('/html/body/div[1]/div[5]/div[2]/table[1]/tbody/tr/td[1]/a/div').screenshot_as_png) 

Sometimes it could get an error because of the scroll, but depending on your necessity, it's a good way to get the image.

like image 37
Ramon Avatar answered Sep 22 '22 11:09

Ramon