Zoeken…


Python Selenium screenshot maken / opslaan van webpagina

from selenium import webdriver

# Create a new cromedriver
driver = webdriver.Chrome()
# Go to www.google.com
driver.get("https://www.google.com")
# Saves a .png file with name my_screenshot_name to the directory that
# you are running the program from.
screenshot_name = "my_screenshot_name.png"
driver.save_screenshot(screenshot_name)

driver.save_screenshot retourneert 'true' als de schermafbeelding is gemaakt en 'false' als dit niet het geval was. Screenshots opslaan werkt ook met browsers zonder hoofd. Als u een screenshot in een andere map wilt opslaan, voegt u gewoon het bestandspad toe (relatief ten opzichte van waar u de code uitvoert). Bijvoorbeeld:

screenshot_name = "screenshots/my_screenshot_name.png"

Zal de screenshot opslaan in map "screenshots" in de map van waaruit python wordt uitgevoerd.

C # TakeScreenshot-extensie

public static Screenshot TakeScreenshot(this IWebDriver _driver)
{
    return ((ITakesScreenshot)_driver).GetScreenshot();
}

Gebruiksvoorbeeld:

driver.TakeScreenshot().SaveAsFile(@"/Test/Test.png",ImageFormat.Png);

Java Selenium screenshot maken / opslaan van webpagina en toevoegen aan rapport

public void Screenshot() throws Throwable{
    final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
    scenario.embed(screenshot, "image/png"); // ... and embed it in the report.
    Thread.sleep(1000);
}

Afwisselend

 public static void captureScreenShot(WebDriver ldriver){

  // Take screenshot and store as a file format
  File src= ((TakesScreenshot)ldriver).getScreenshotAs(OutputType.FILE);
try {
  // now copy the  screenshot to desired location using copyFile method

 FileUtils.copyFile(src, new File("C:/selenium/"+System.currentTimeMillis()+".png"));
   }

catch (IOException e)

{ 
System.out.println(e.getMessage());
  }
}


Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow