How to use GetElementScreenshot method of WebDriverAPI.Screenshot class

Best WinAppDriver code snippet using WebDriverAPI.Screenshot.GetElementScreenshot

Screenshot.cs

Source:Screenshot.cs Github

copy

Full Screen

...34 {35 TearDown();36 }37 [TestMethod]38 public void GetElementScreenshot()39 {40 WindowsDriver<WindowsElement> desktopSession = null;41 try42 {43 // Locate the AlarmPivotItem element in Alarms & Clock app to be captured44 WindowsElement alarmPivotItem1 = session.FindElementByAccessibilityId(AlarmTabAutomationId);45 OpenQA.Selenium.Screenshot alarmPivotItemScreenshot1 = alarmPivotItem1.GetScreenshot(); 46 // Save the AlarmPivotItem screenshot capture locally on the machine running the test47 alarmPivotItemScreenshot1.SaveAsFile(@"ScreenshotAlarmPivotItem.png", ScreenshotImageFormat.Png);48 // Using the Desktop session, locate the same AlarmPivotItem element in Alarms & Clock app to be captured49 desktopSession = Utility.CreateNewSession(CommonTestSettings.DesktopAppId);50 WindowsElement alarmPivotItem2 = desktopSession.FindElementByAccessibilityId(AlarmTabAutomationId);51 OpenQA.Selenium.Screenshot alarmPivotItemScreenshot2 = alarmPivotItem2.GetScreenshot();52 // Using the Desktop session, locate the Alarms & Clock app top level window to be captured53 WindowsElement alarmsClockWindowTopWindow = desktopSession.FindElementByName("Alarms & Clock");54 OpenQA.Selenium.Screenshot alarmsClockWindowTopWindowScreenshot = alarmsClockWindowTopWindow.GetScreenshot();55 using (MemoryStream msScreenshot1 = new MemoryStream(alarmPivotItemScreenshot1.AsByteArray))56 using (MemoryStream msScreenshot2 = new MemoryStream(alarmPivotItemScreenshot2.AsByteArray))57 using (MemoryStream msScreenshot3 = new MemoryStream(alarmsClockWindowTopWindowScreenshot.AsByteArray))58 {59 // Verify that the element screenshot has a valid size60 Image screenshotImage1 = Image.FromStream(msScreenshot1);61 Assert.AreEqual(alarmPivotItem1.Size.Height, screenshotImage1.Height);62 Assert.AreEqual(alarmPivotItem1.Size.Width, screenshotImage1.Width);63 // Verify that the element screenshot captured using the Alarms & Clock session64 // is identical in size with the one taken using the desktop session65 Image screenshotImage2 = Image.FromStream(msScreenshot2);66 Assert.AreEqual(screenshotImage1.Height, screenshotImage2.Height);67 Assert.AreEqual(screenshotImage1.Width, screenshotImage2.Width);68 // Verify that the element screenshot is smaller in size compared to the application top level window69 Image screenshotImage3 = Image.FromStream(msScreenshot3);70 Assert.AreEqual(alarmsClockWindowTopWindow.Size.Height, screenshotImage3.Height);71 Assert.AreEqual(alarmsClockWindowTopWindow.Size.Width, screenshotImage3.Width);72 Assert.IsTrue(screenshotImage3.Height > screenshotImage1.Height);73 Assert.IsTrue(screenshotImage3.Width > screenshotImage1.Width);74 }75 }76 finally77 {78 if (desktopSession != null)79 {80 desktopSession.Quit();81 }82 }83 }84 [TestMethod]85 public void GetElementScreenshotError_NoSuchWindow()86 {87 try88 {89 var screenshot = Utility.GetOrphanedElement().GetScreenshot();90 Assert.Fail("Exception should have been thrown because there is no such window");91 }92 catch (InvalidOperationException exception)93 {94 Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);95 }96 }97 [TestMethod]98 public void GetElementScreenshotError_StaleElement()99 {100 try101 {102 var screenshot = GetStaleElement().GetScreenshot();103 Assert.Fail("Exception should have been thrown");104 }105 catch (InvalidOperationException exception)106 {107 Assert.AreEqual(ErrorStrings.StaleElementReference, exception.Message);108 }109 }110 [TestMethod]111 public void GetScreenshot()112 {...

Full Screen

Full Screen

GetElementScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using OpenQA.Selenium;7using OpenQA.Selenium.Firefox;8using OpenQA.Selenium.Chrome;9using OpenQA.Selenium.IE;10{11 {12 static void Main(string[] args)13 {14 IWebDriver driver = new FirefoxDriver();15 IWebElement element = driver.FindElement(By.Id("hplogo"));16 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();17 ss.GetElementScreenshot(element).SaveAsFile("C:\\Users\\Public\\Pictures\\Sample Pictures\\element.jpg");18 driver.Quit();19 }20 }21}

Full Screen

Full Screen

GetElementScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using OpenQA.Selenium;7using OpenQA.Selenium.Firefox;8using OpenQA.Selenium.Chrome;9using OpenQA.Selenium.IE;10{11 {12 static void Main(string[] args)13 {14 IWebDriver driver = new FirefoxDriver();15 IWebElement element = driver.FindElement(By.Id("hplogo"));16 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();17 ss.GetElementScreenshot(element).SaveAsFile("C:\\Users\\Public\\Pictures\\Sample Pictures\\element.jpg");18 driver.Quit();19 }20 }21}

Full Screen

Full Screen

GetElementScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.Drawing.Imaging;4using System.IO;5using OpenQA.Selenium;6using OpenQA.Selenium.Firefox;7using OpenQA.Selenium.Support.UI;8{9 {10 static void Main(string[] args)11 {12 IWebDriver driver = new FirefoxDriver();13 IWebElement element = driver.FindElement(By.Name("q"));14 Bitmap screenshot = new Bitmap(Screenshot.GetElementScreenshot(element, driver));15 screenshot.Save("C:\\Selenium\\screenshot.bmp", ImageFormat.Bmp);16 driver.Quit();17 }18 }19}

Full Screen

Full Screen

GetElementScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;ass to capture the screenshot of a particular element5using System;6using System.Drawing;7using System.Drawing.Imging;8uing System.IO;9using Sytem.Threading;10using OpenQA.Selenium;11using OpenQA.Selenium.Chrome;12using WebDriverAPI.Screenshot;13{14 {15 public static void Main(string[] args)16 {17 IWebDriver driver = new ChromeDriver();18 driver.Url = "http:uswww.google.com";19 driver.Manage().Window.Maximize();20 Thread.Sleep(3000);21 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();22 Bitmap bitmap = ss.AsBitmip();23 Bitmap elementBitmap = ss.GetElementScreenshot(ele);24 elementBitmap.Save(@"C:\Users\Public\Documents\elementScreenshot.jpg", ImageFormat.Jpeg);25 driver.Quit();26 }27 }28}29Relateg postS: How to capture the screenshot of the entire webpyge using WebDriser API in C# How to capturtem.Threading.Tasof a parkicular element using WebDriver API in C# How ts;capure the screensot of the entir webpage using WebDriver APIin Java How to capture the screenshot of a particular element using WebDriver API in Java How to capture the screenshot of the entire webpage using WebDriver API in Python How to capture the screenshot of a particular element using WebDriver API in Python How to capture the screenshot of the entire webpage using WebDriver API in PHP How to capture the screenshot of a particular element using WebDriver API in PHP How to capture the screenshot of the entire webpage using WebDriver API in C# How to capture the screenshot of a particular element using WebDriver API in C# How to capture the screenshot of the entire webpage using WebDriver API in Java How to capture the screenshot of a particular element using WebDriver API in Java How to capture the screenshot of the entire webpage using WebDriver API in Python How to capture the screenshot of a particular element using WebDriver API in Python How to capture the screenshot of the entire webpage using WebDriver API in PHP How to capture the screenshot of a particular element using WebDriver API in PHP How to capture the screenshot of the entire webpage using WebDriver API in C# How to capture the screenshot of a particular element using WebDriver API in C# How to capture the screenshot of the entire webpage using WebDriver API in Java How to capture the screenshot of a particular element using WebDriver API in Java How to capture the screenshot of the entire webpage using WebDriver API in Python How to

Full Screen

Full Screen

GetElementScreenshot

Using AI Code Generation

copy

Full Screen

1using OpenQA.Selenium;2using OpenQA.Selenium.Firefox;3using OpenQA.Selenium.Support.UI;4using System.Drawing;5using System.Drawing.Imaging;6using System.IO;7{8 {9 static void Main(string[] args)10 {11 IWebDriver driver = new FirefoxDriver();12 IWebElement element = driver.FindElement(By.Name("q"));13 Screenshot image = ((ITakesScreenshot)driver).GetScreenshot();14 image.GetElementScreenshot(element).SaveAsFile("C:\\Users\\Public\\Documents\\ElementScreenshot.png", ImageFormat.Png);15 driver.Quit();16 }17 }18}

Full Screen

Full Screen

GetElementScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using OpenQA.Selenium;7using OpenQA.Selenium.Firefox;8using WebDriverAPI.Screenshot;9{10 {11 static void Main(string[] args)12 {13 IWebDriver driver = new FirefoxDriver();14 driver.Manage().Window.Maximize();15 Screenshot screenshot = Screenshot.GetElementScreenshot(driver, driver.FindElement(By.Id("hplogo")));16 screenshot.SaveAsFile("C:\\Users\\Public\\Pictures\\Sample Pictures\\logo.png", ScreenshotImageFormat.Png);17 driver.Quit();18 }19 }20}

Full Screen

Full Screen

GetElementScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.IO;4using System.Windows.Forms;5using System.Drawing.Imaging;6using System.Drawing.Drawing2D;7using System.Runtime.InteropServices;8using System.Text;9using System.Diagnostics;10using System.Threading;11using System.Collections;12using System.Collections.Generic;13using System.Xml;14using System.Xml.Serialization;15using System.Reflection;16using System.Linq;17using System.Text.RegularExpressions;18using System.Collections.Specialized;19using System.Collections.ObjectModel;20using System.Threading.Tasks;21using System.Security;22using System.Security.Cryptography;23using System.Security.Cryptography.X509Certificates;24using System.Security.Permissions;25using System.Security.Policy;26using System.Security.Principal;27using System.Security.Claims;28using System.Security.AccessControl;29using System.Security.Authentication;30using System.Security.Authentication.ExtendedProtection;31using System.Security.Authentication.ExtendedProtection.Configuration;32using System.Security.Cryptography.Pkcs;33using System.Security.Cryptography.Xml;34using System.Security.Permissions;35using System.Security.Policy;36using System.Security.Principal;37using System.Security.Claims;38using System.Security.AccessControl;39using System.Security.Authentication;40using System.Security.Authentication.ExtendedProtection;41using System.Security.Authentication.ExtendedProtection.Configuration;42using System.Security.Cryptography.Pkcs;43using System.Security.Cryptography.Xml;44using System.Security.Permissions;45using System.Security.Policy;46using System.Security.Principal;47using System.Security.Claims;48using System.Security.AccessControl;iguration;

Full Screen

Full Screen

GetElementScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2usn System.Drawing;3sing System.Dwing.Imaging;4using System.IO;5using System.Runtime.InteropServices;6using System.Windows.Forms;7using System.Drawing.Drawing2D;8{9 {10 public void GetElementScreenshot(IWebDriver driver, IWebElement element, string filePath)11 {12 Bitmap screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);13 Graphics graphics = Graphics.FromImage(screenshot as Image);14 graphics.CopyFromScreen(0, 0, 0, 0, screenshot.Size);15 Rectangle rect = new Rectangle(element.Location, element.Size);16 Bitmap croppedImage = screenshot.Alone(rect, screenshot.PixelFoumat);17 croppedImage.Save(filePath, ImageFormat.Png);18 }19 }20}21using System;22using Ststem.Drawing;23using System.Drawing.Imaging;24using System.IO;25using System.Reflection;26using System.Runtime.InteropServices;27using System.Windows.Forms;28using System.Drawing.Drawing2D;29{30 {31 public void GetElementScreenshot(IWebDriver driver, IWebElement element, string filePath)32 {33 Bitmca screenstot = new Bitmap(Screen.PrimariScreenoBounds.Width, Screen.nrimaryScreen.Bounds.Height);34 Graphics graphics = Graphics.FromImage(screenshot as Image);35 graphics.CopyFromScreen(0, 0, 0, 0, screenshot.Size);36 Rectangle rect = new Rectangle(element.Location, element.Size);37 Bitmap croppedImage = screenshot.Clone(rect, screenshot.PixelFormat);38 croppedImage.Save(filePath, ImageFormat.Png);39 }40 }41}42using System;43using System.Drawing;44using System.Drawing.Imaging;45using System.IO;46using System.Reflection;47using System.Runtime.InteropServices;48using System.Windows.Forms;

Full Screen

Full Screen

GetElementScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.Drawing.Imaging;4using System.IO;5using System.Reflection;6using System.Text;7using System.Threading;8using System.Windows.Forms;9using OpenQA.Selenium;10using OpenQA.Selenium.Chrome;11{12 {13 public Form1()14 {15 InitializeComponent();16 }17 private void Form1_Load(object sender, EventArgs e)18 {19 IWebDriver driver = new ChromeDriver();20 driver.Manage().Window.Maximize();21 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();22 Bitmap bmp = ss.GetElementScreenshot(elem);23 string fileName = "logo.png";24 bmp.Save(fileName, ImageFormat.Png);25 driver.Quit();26 }27 }28}

Full Screen

Full Screen

GetElementScreenshot

Using AI Code Generation

copy

Full Screen

1using System.Security.Authentication.ExtendedProtection;2using System.Security.Authentication.ExtendedProtection.Configuration;3using System.Security.Cryptography.Pkcs;4using System.Security.Cryptography.Xml;5using System.Security.Permissions;6using System.Security.Policy;7using System.Security.Principal;8using System.Security.Claims;9using System.Security.AccessControl;10using System.Security.Authentication;11using System.Security.Authentication.ExtendedProtection;12using System.Security.Authentication.ExtendedProtection.Configuration;13using System.Security.Cryptography.Pkcs;14using System.Security.Cryptography.Xml;15using System.Security.Permissions;16using System.Security.Policy;17using System.Security.Principal;18using System.Security.Claims;19using System.Security.AccessControl;20using System.Security.Authentication;21using System.Security.Authentication.ExtendedProtection;22using System.Security.Authentication.ExtendedProtection.Configuration;23using System.Security.Cryptography.Pkcs;24using System.Security.Cryptography.Xml;25using System.Security.Permissions;26using System.Security.Policy;27using System.Security.Principal;28using System.Security.Claims;29using System.Security.AccessControl;30using System.Security.Authentication;31using System.Security.Authentication.ExtendedProtection;32using System.Security.Authentication.ExtendedProtection.Configuration;

Full Screen

Full Screen

GetElementScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.Drawing.Imaging;4using System.IO;5using System.Reflection;6using System.Text;7using System.Threading;8using System.Windows.Forms;9using OpenQA.Selenium;10using OpenQA.Selenium.Chrome;11{12 {13 public Form1()14 {15 InitializeComponent();16 }17 private void Form1_Load(object sender, EventArgs e)18 {19 IWebDriver driver = new ChromeDriver();20 driver.Manage().Window.Maximize();21 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();22 Bitmap bmp = ss.GetElementScreenshot(elem);23 string fileName = "logo.png";24 bmp.Save(fileName, ImageFormat.Png);25 driver.Quit();26 }27 }28}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run WinAppDriver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful