How to take a screenshot using python3 and selenium

I want to take a screenshot. I tried with the code below:

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

but got an 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)
0
source share
2 answers

A few problems:

  • find_elements_by_xpathreturns a list, and you cannot request a screenshot in the list. Did you mean find_element_by_xpath?
  • In any case, with the exception of Microsoft Edge, no, WebDriver actually implements element-based screenshots . You must create a screenshot with a full window and crop it.

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

+3
source

With Firefox, you can:

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

, id find_element_by_css_selector.

+1

Source: https://habr.com/ru/post/1672372/


All Articles