How can I take a screenshot with Selenium WebDriver?

后端 未结 30 2578
不知归路
不知归路 2020-11-21 07:48

Is it possible to take a screenshot using Selenium WebDriver?

(Note: Not Selenium Remote Control)

30条回答
  •  清酒与你
    2020-11-21 08:24

    Python

    Each WebDriver has a .save_screenshot(filename) method. So for Firefox, it can be used like this:

    from selenium import webdriver
    
    browser = webdriver.Firefox()
    browser.get('http://www.google.com/')
    browser.save_screenshot('screenie.png')
    

    Confusingly, a .get_screenshot_as_file(filename) method also exists that does the same thing.

    There are also methods for: .get_screenshot_as_base64() (for embedding in HTML) and .get_screenshot_as_png()(for retrieving binary data).

    And note that WebElements have a .screenshot() method that works similarly, but only captures the selected element.

提交回复
热议问题