selenium
Prenez une capture d'écran d'une page Web
Recherche…
Capture d'écran Python Selenium Take / Save de la page Web
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 renvoie "true" si la capture d'écran a été prise et "false" si ce n'est pas le cas. L'enregistrement des captures d'écran fonctionne également avec les navigateurs sans tête. Si vous souhaitez enregistrer une capture d'écran dans un répertoire différent, ajoutez simplement le chemin du fichier (relatif à l'endroit où vous exécutez le code). Par exemple:
screenshot_name = "screenshots/my_screenshot_name.png"
Sauvegarde la capture d'écran dans le répertoire "screenshots" dans le répertoire à partir duquel python est exécuté.
Extension C # TakeScreenshot
public static Screenshot TakeScreenshot(this IWebDriver _driver)
{
return ((ITakesScreenshot)_driver).GetScreenshot();
}
Exemple d'utilisation:
driver.TakeScreenshot().SaveAsFile(@"/Test/Test.png",ImageFormat.Png);
Capture d'écran de sélection / enregistrement Java de la page Web et ajout d'un 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);
}
Alternativement
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
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow