python selenium从元素保存截图

时间:2017-04-19 22:27:05

标签: python selenium screenshot

这是我试过的代码,我是从另一个stackoverflow页面得到的,但是它没有用。任何想法为什么?

chrome = webdriver.Chrome()
chrome.get('http://stackoverflow.com/')

# now that we have the preliminary stuff out of the way time to get that image :D
element = chrome.find_element_by_class_name('-img') # find part of the page you want image of
location = element.location
size = element.size
chrome.save_screenshot('screenshot.png') # saves screenshot of entire page
chrome.quit()
  

引发exception_class(消息,屏幕,堆栈跟踪)   selenium.common.exceptions.WebDriverException:消息:未知错误:无法获得自动化扩展   来自未知错误:找不到页面:chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html     (会话信息:chrome = 57.0.2987.133)     (驱动程序信息:chromedriver = 2.25.426923(0390b88869384d6eb0d5d09729679f934aab9eed),platform = Windows NT 10.0.10586 x86_64)

编辑: 试图

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome = webdriver.Chrome(chrome_options=chrome_options)

chrome.get('http://stackoverflow.com/')

# now that we have the preliminary stuff out of the way time to get that image :D
element = chrome.find_element_by_class_name('-img') # find part of the page you want image of
location = element.location
size = element.size
chrome.save_screenshot('screenshot.png') # saves screenshot of entire page
chrome.quit()

错误:

  

引发exception_class(消息,屏幕,堆栈跟踪)   selenium.common.exceptions.WebDriverException:消息:未知错误:无法获得自动化扩展   来自未知错误:找不到页面:chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html     (会话信息:chrome = 57.0.2987.133)     (驱动程序信息:chromedriver = 2.25.426923(0390b88869384d6eb0d5d09729679f934aab9eed),platform = Windows NT 10.0.10586 x86_64)

1 个答案:

答案 0 :(得分:0)

你应该更新你的chromedriver。我认为你的镀铬驱动程序已经过时了。

根据你的问题,你想截图一个元素,目前selenium提供了一种方法来做到这一点

只需致电element.save_screenshot("file name"),它就会捕获特定元素的截图