Python + selenium download image without extension

700 views Asked by At

I'm using python 3 with selenium, I have to download an image

HTML:

<img id="labelImage" name="labelImage" border="0" width="672" height="456" alt="labelImage" src="/shipping/labelAction.handle?method=doGetLabelFromCache&amp;isDecompressRequired=false&amp;utype=null&amp;cacheKey=774242409034SHIPPING_L">

Python code:

found = browser.find_element_by_css_selector('img[alt="labelImage"]') 
src = found.get_attribute('src')
urllib.request.urlretrieve(src, 'image.png')

that image file is empty, if I try to switch extension to html, shows me message below: "We're sorry, we can't process your request right now. It appears you don't have permission to view this webpage"

1

There are 1 answers

0
Todor Minakov On BEST ANSWER

The error you recieve when attempt to download comes from the fact the urllib call is a brand new session for their server - it does not have the cookies and authentication your browser does. E.g. it is the same as if you open incognito mode in the browser, and paste in the address bar the src attribute - for the server you are a new client, that hasn't fill in the form, logged in, etc.

You may want to try something else - in the selenium/the browser session, taking a screenshot of just the <img> element. That op is with variable success, Chrome for instance added support for it only recently, and in some situations it fails:

found = browser.find_element_by_css_selector('img[alt="labelImage"]')
try:
    found.screenshot('element.png')
except Exception as ex:  # FIXME: anti-pattern - I don't recall the exact exception - when you run the code, change it to the proper one
    print('The correct exception is {}'.format(ex))
    browser.get_screenshot_as_file('page.png')

If taking the element's screenshot fails, you'll get one of the whole page - which you can then trim to the element.