0

Trying to take a screenshot of a captcha at the following site: https://servicesenligne2.ville.montreal.qc.ca/sel/evalweb/index

I'm trying to screen scrape because I can't seem to download the picture.

See code below:

from selenium import webdriver
from PIL import Image

fox = webdriver.Firefox()
fox.get('https://servicesenligne2.ville.montreal.qc.ca/sel/evalweb/index')
fox.maximize_window()

element = fox.find_element_by_xpath("//img[contains(@src,'createimage.png?timestamp=')]")

location = element.location
size = element.size

fox.get_screenshot_as_file('screenshot.png')
save_screenshot('screenshot.png')

im = Image.open('screenshot.png') # uses PIL library to open image in memory

left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']

im = im.crop((left, top, right, bottom)) # defines crop points
im.save('screenshot.png') # saves new cropped image
RageAgainstheMachine
  • 901
  • 2
  • 11
  • 28
  • *I can't seem to download the picture* ...how did you try? – Andersson Mar 13 '18 at 14:19
  • You're saving the screenshot twice and then opening it again, before cropping it and re-saving. There's no need to do that. Also, you're not taking the browser chrome into account when you're calculating your crop area. – Aaron F Mar 13 '18 at 14:20
  • @ Aaron, how do I take the browser into account when calculating the crop area? – RageAgainstheMachine Mar 13 '18 at 14:26
  • @Andersson Please see question here: https://stackoverflow.com/questions/49258718/downloading-image-using-selenium-in-python-3 – RageAgainstheMachine Mar 13 '18 at 14:27
  • What happens, any exceptions? You have changed the source code taken from https://stackoverflow.com/questions/15018372/how-to-take-partial-screenshot-with-selenium-webdriver-in-python. Why? `fox.save_screenshot('screenshot.png')` should do the job. – Alexey Dolgopolov Mar 13 '18 at 15:08

1 Answers1

0

The captcha image doesn't appear in browser window with default dimensions 800x600. You need to resize window with the following:

fox.set_window_size(1800, 1024)
fox.maximize_window()
fox.save_screenshot('screenshot.png')