本文介绍了如何使用python3和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')
但出现错误:
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)
推荐答案
几个问题:
-
find_elements_by_xpath
返回一个列表,您不能在列表上请求屏幕截图.您是说find_element_by_xpath
吗? - 在任何情况下,除Microsoft Edge以外,没有WebDriver实际上实现基于元素的屏幕截图.您必须生成一个完整的窗口截图并将其裁剪.
find_elements_by_xpath
returns a list, and you can't request a screenshot on a list. Did you meanfind_element_by_xpath
?- 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.
这篇关于如何使用python3和selenium拍摄元素的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!