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&isDecompressRequired=false&utype=null&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"
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:If taking the element's screenshot fails, you'll get one of the whole page - which you can then trim to the element.