How to take screenshot of element using python3 and selenium

I want to take screenshot of the element. I tried with below mentioned code:

ElementIs=driver.find_element_by_xpath(".//body/img")
ElementIs.screenshot('abc.jpg')

but got error:

File "C:\Python34\lib\site-packages\selenium-2.47.1-y3.4.egg\selenium\webdriver\remote\webelement.py", line 448, in _execute   return self._parent.execute(command, params)
File "C:\Python34\lib\site-packages\selenium-2.47.1-y3.4.egg\selenium\webdriver\remote\webdriver.py", line 196, in execute self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium-2.47.1-py3.4.egg\selenium\webdriver\remote\errorhandler.py", line 102, in check_responsevalue = 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)

Answers


A couple of problems:

  1. find_elements_by_xpath returns a list, and you can't request a screenshot on a list. Did you mean find_element_by_xpath?
  2. In any case, with the exception of Microsoft Edge, no WebDriver actually implements element-based screenshots. You have to generate a full-window screenshot and crop it.

See my earlier answer on this topic for more background and workarounds.


With Firefox you can:

driver = webdriver.Firefox()
qr = driver.find_element_by_css_selector('.css_class')
qr.screenshot("abc.jpg")

You can use class, id or tags in find_element_by_css_selector.


Need Your Help

Dropwizard user registration (basic, social, OpenID, OAuth, Google+)

authentication oauth dropwizard

I am writing my first Dropwizard based application. It's a proof-of-concept for a somewhat skeptical boss at a well established php shop.

making numpy.nanargmin return nan if column is all nan

python numpy nan

Is it possible to use numpy.nanargmin, so that it returns numpy.nan, on columns where there are only nans in them. Right now, it raises a ValueError, when that happens. And i cant use numpy.argmin,...