0

I'm trying to find the way of how to get screenshot of particular webelement with Selenium WebDriver and I was happy to know that starting from v2.46.1 screenshot() method is available and it should provide ability to resolve my issue... But I can't actually apply it as I get following trace:

>>>from selenium import webdriver
>>>driver = webdriver.Chrome()
>>>image_to_save = driver.find_element_by_xpath("//img[@alt='Some picture']")
>>>image_to_save.screenshot('C\\Users\\Me\\Desctop\\Image.png')

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 405, in screenshot
png = self.screenshot_as_png
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 392, in screenshot_as_png
return base64.b64decode(self.screenshot_as_base64.encode('ascii'))
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 382, in screenshot_as_base64
return self._execute(Command.ELEMENT_SCREENSHOT)['value']
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 454, in _execute
return self._parent.execute(command, params)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 102, in check_response
value = json.loads(value_json)
File "C:\Python34\lib\json\__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "C:\Python34\lib\json\decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

Am I doing something wrong or this method wasn't implemented correctly?

Updated with full log

P.S. Windows 7, Selenium 2.48, Python 3.4.0

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Please post the full traceback error message so that it is clear on what line that `ValueError` occurs. – unutbu Nov 10 '15 at 14:15
  • @unutbu, It was written in Python command line, so the problem occured in last line – Andersson Nov 10 '15 at 14:29
  • 1
    According to https://github.com/SeleniumHQ/selenium/issues/912 the `element.screenshot` method hasn't been implemented for all drivers -- so far, it is only implemented for Marionette and EdgeDriver. – unutbu Nov 10 '15 at 15:18
  • @unutbu, oh... that's bad. thanks for info – Andersson Nov 10 '15 at 16:28
  • I have code to take screenshot of particular web element, but i don't know python. i used Java Binding. if you could import matching library files may be you can try? – Mona Nov 10 '15 at 17:55
  • Possible duplicate of [How to take partial screenshot with Selenium WebDriver in python?](http://stackoverflow.com/questions/15018372/how-to-take-partial-screenshot-with-selenium-webdriver-in-python) – JeffC Nov 11 '15 at 07:06
  • @JeffC, These are definitely different issues: question is "How a particular method works", but not "How to take a screenshot"! – Andersson Nov 11 '15 at 09:20

1 Answers1

0

What you are doing is completely wrong.

You can simply call the webdriver.browser.save_screenshot(path_to_save_the_screenshot) where browser is anything like Firefox, Chrome or Ie. It will take the screenshot of the current page. You cannot focus on a certain element and take its screenshot.

Abhinav
  • 992
  • 2
  • 11
  • 26
  • Moreover, I can use `get_screenshot_as_base64()` to save screen as simple string variable, but the goal is more complex... – Andersson Nov 17 '15 at 16:45
  • Depends on your need. If you need graphics I would say use `webdriver.browser.save_screenshot("~/some_path/some_screenshot.png")` Otherwise, just like you pointed out, a b64 to have a simple string file. – Abhinav Nov 17 '15 at 17:31
  • Did it help you achieve the goal? – Abhinav Nov 18 '15 at 11:31
  • No. Your answer does not correspond to my question – Andersson Nov 18 '15 at 16:28
  • With all due respect, my answer is not irrelevant. This feature is not available as I mentioned and you cannot take the screenshot of a WebElement as explained in here as well https://github.com/SeleniumHQ/selenium/issues/912 – Abhinav Nov 19 '15 at 09:48
  • This question was only about certain `selenium` method. `unutbu` user said in comments that this method works only for two browsers and this answer is more than enough for me now. I already know about taking a whole page screenshot and it not suits me as it will not resolve issue... However thanks for your suggestions – Andersson Nov 19 '15 at 12:22
  • Very true what `ubuntu` said. Marionette and Edge are the only two drivers supported. Firefox and IE have been included in this PR while Chrome is still a long way off. – Abhinav Nov 19 '15 at 12:50