Webdriver 截图

问题描述

用python在windows上使用Selenium Webdriver截图时,截图直接保存到程序的路径下,有没有办法将.png文件保存到特定目录?

When taking a screenshot using Selenium Webdriver on windows with python, the screenshot is saved directly to the path of the program, is there a way to save the .png file to a specific directory?


解决方案

使用 driver.save_screenshot('/path/to/file')driver.get_screenshot_as_file('/path/到/文件'):

import selenium.webdriver as webdriver
import contextlib

@contextlib.contextmanager
def quitting(thing):
    yield thing
    thing.quit()

with quitting(webdriver.Firefox()) as driver:
    driver.implicitly_wait(10)
    driver.get('http://www.google.com')
    driver.get_screenshot_as_file('/tmp/google.png') 
    # driver.save_screenshot('/tmp/google.png')

相关文章