我想截取元素的截图。 我尝试了下面提到的代码:
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)
答案 0 :(得分:3)
有几个问题:
find_elements_by_xpath
会返回一个列表,您无法在列表中请求屏幕截图。你的意思是find_element_by_xpath
?有关更多背景信息和解决方法,请参阅此主题的my earlier answer。
答案 1 :(得分:1)
使用Firefox,您可以:
driver = webdriver.Firefox()
qr = driver.find_element_by_css_selector('.css_class')
qr.screenshot("abc.jpg")
您可以在find_element_by_css_selector中使用class,id或tags。