1

I am new to selenium module in python. I am trying to load image and save it locally. For that I am using following code

driver = webdriver.Firefox()
driver.execute_script("window.open()" )
driver.switch_to_window(driver.window_handles[1])
driver.get("http://media.santabanta.com/medium1/cricket/trent%20boult/trent-boult-0a.jpg")
driver.save_screenshot('im1.jpg')

The problem I am getting is the saved image is empty(0 bytes).

5
  • Is the url a protected URL? I'm not able to access this image via the URL directly. This might explain why file is of 0 KBs. Commented Jun 18, 2017 at 7:45
  • add some implicit wait and try. We can also use driver.get_screenshot_as_file('/tmp/google.png') Commented Jun 18, 2017 at 7:47
  • @demouser123 No, its not. Are you opening it in your browser ? Commented Jun 18, 2017 at 9:14
  • @santhoshkumar driver.get_screenshot_as_file is having same problem. Commented Jun 18, 2017 at 9:15
  • Can you add some implicit wait and some sleep and try? Is the screenshot working for other sites? Commented Jun 18, 2017 at 12:10

1 Answer 1

1

My answer cannot solve your current issue, but if you want just to save picture you can avoid using selenium and simply use below piece of code:

import urllib

urllib.request.urlretrieve("http://media.santabanta.com/medium1/cricket/trent%20boult/trent-boult-0a.jpg", 'path/to/im1.jpg')

P.S. In Python 2.X urllib.urlretrieve() should be used instead of urllib.request.urlretrieve()

Sign up to request clarification or add additional context in comments.

1 Comment

Although this method worked for me, but I dont know whether I should accept this answer since the question is about Selenium

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.