10 Advanced WebDriver Tips and Tricks Part 1

10 Advanced WebDriver Tips and Tricks Part 1

As you probably know I am developing a series posts called- Pragmatic Automation with WebDriver. They consist of tons of practical information how to start writing automation tests with WebDriver. Also, contain a lot of more advanced topics such as automation strategies, benchmarks and researches. In the next couple of publications, I am going to share with you some Advanced WebDriver usages in tests. Without further ado, here are the today’s advanced WebDriver Automation tips and trips.

1. Taking a Screenshot

You can use the following method to take a full-screen screenshot of the browser.

public void TakeFullScreenshot(IWebDriver driver, String filename)
{
    Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
    screenshot.SaveAsFile(filename, ImageFormat.Png);
}

Sometimes you may need to take a screenshot of a single element.

public void TakeScreenshotOfElement(IWebDriver driver, By by, string fileName)
{
    // 1. Make screenshot of all screen
    var screenshotDriver = driver as ITakesScreenshot;
    Screenshot screenshot = screenshotDriver.GetScreenshot();
    var bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));
    // 2. Get screenshot of specific element
    IWebElement element = driver.FindElement(by);
    var cropArea = new Rectangle(element.Location, element.Size);
    var bitmap = bmpScreen.Clone(cropArea, bmpScreen.PixelFormat);
    bitmap.Save(fileName);
}

First we make a full-screen screenshot then we locate the specified element by its location and size attributes. After that, the found rectangle chunk is saved as a bitmap.

Here is how you use both methods in tests.


public void WebDriverAdvancedUsage_TakingFullScrenenScreenshot()
{
    this.driver.Navigate().GoToUrl(@"http://automatetheplanet.com");
    this.WaitUntilLoaded();
    string tempFilePath = Path.GetTempFileName().Replace(".tmp", ".png");
    this.TakeFullScreenshot(this.driver, tempFilePath);
}

public void WebDriverAdvancedUsage_TakingElementScreenshot()
{
    this.driver.Navigate().GoToUrl(@"http://automatetheplanet.com");
    this.WaitUntilLoaded();
    string tempFilePath = Path.GetTempFileName().Replace(".tmp", ".png");
    this.TakeScreenshotOfElement(this.driver,
    By.XPath("//*[@id='tve_editor']/div[2]/div[2]/div/div"), tempFilePath);
}

We get a temp file name through the special Path .NET class. By default temp files are generated with “.tmp” extension because of that we replace it with “.png”.

2. Get HTML Source of WebElement

You can use the GetAttribute method of the IWebElement interface to get the inner HTML of a specific element.


public void GetHtmlSourceOfWebElement()
{
    this.driver.Navigate().GoToUrl(@"http://automatetheplanet.com");
    this.WaitUntilLoaded();
    var element =
    this.driver.FindElement(By.XPath("//*[@id='tve_editor']/div[2]/div[3]/div/div"));
    string sourceHtml = element.GetAttribute("innerHTML");
    Debug.WriteLine(sourceHtml);
}

While ago when we were working on the first version of the BELLATRIX test automation framework, I did this research and afterward we used many of the tricks in lots of the features of our solution.

3. Execute JavaScript

You can use the interface IJavaScriptExecutor to execute JavaScript through WebDriver.


public void ExecuteJavaScript()
{
    this.driver.Navigate().GoToUrl(@"http://automatetheplanet.com");
    this.WaitUntilLoaded();
    IJavaScriptExecutor js = driver as IJavaScriptExecutor;
    string title = (string)js.ExecuteScript("return document.title");
    Debug.WriteLine(title);
}

This test is going to take the current window’s title via JavaScript and print it to the Debug Output Window.

4. Set Page Load Timeout

There are at least three methods that you can use for the job.

First you can set the default driver’s page load timeout through the WebDriver’s options class.

this.driver.Manage().Timeouts().SetPageLoadTimeout(new TimeSpan(0, 0, 10));

You can wait until the page is completely loaded via JavaScript.

private void WaitUntilLoaded()
{
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
    wait.Until((x) =>
    {
        return ((IJavaScriptExecutor)this.driver)
    .ExecuteScript("return document.readyState").Equals("complete");
    });
}

Your third option is to wait for a specific element(s) to be visible on the page.

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(
By.XPath("//*[@id='tve_editor']/div[2]/div[2]/div/div")));

Headless Browser

5. Execute Tests in a Headless Browser

PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG. In order to be able to use the PhantomJSDriver in your code, you first need to download its binaries.


public void ExecuteInHeadlessBrowser()
{
    this.driver = new PhantomJSDriver(
    @"D:ProjectsPatternsInAutomation.TestsWebDriver.Series.TestsDrivers");
    this.driver.Navigate().GoToUrl(@"http://automatetheplanet.com");
    this.WaitUntilLoaded();
    IJavaScriptExecutor js = driver as IJavaScriptExecutor;
    string title = (string)js.ExecuteScript("return document.title");
    Debug.WriteLine(title);
}

This test is executed under 3 seconds through the PhantomJSDriver. Almost three times faster than with the FirefoxDriver.

6. Check If an Element Is Visible

You can use the Displayed property of the IWebElement interface.


public void CheckIfElementIsVisible()
{
    this.driver.Navigate().GoToUrl(@"http://automatetheplanet.com");
    Assert.IsTrue(driver.FindElement(
    By.XPath("//*[@id='tve_editor']/div[2]/div[2]/div/div")).Displayed);
}

7. Use Specific Profile

By default WebDriver always assigns a new ‘clean’ profile if you use the FirefoxDriver’s default constructor. However, sometimes you may want to fine-tune the profile e.g. add extensions, turn off the JavaScript, etc.

FirefoxProfileManager profileManager = new FirefoxProfileManager();
FirefoxProfile profile = profileManager.GetProfile("YourProfileName");
this.driver = new FirefoxDriver(profile);

You can do some similar configurations for the ChromeDriver.

ChromeOptions options = new ChromeOptions();
// set some options
DesiredCapabilities dc = DesiredCapabilities.Chrome();
dc.SetCapability(ChromeOptions.Capability, options);
IWebDriver driver = new RemoteWebDriver(dc);

8. Turn Off JavaScript

You can use the code from point 7 to set up a new Firefox profile. Then you need to set the ‘javascript.enabled’ attribute to false.

FirefoxProfileManager profileManager = new FirefoxProfileManager();
FirefoxProfile profile = profileManager.GetProfile("HARDDISKUSER");
profile.SetPreference("javascript.enabled", false);
this.driver = new FirefoxDriver(profile);

Manage Cookies

9. Manage Cookies

Before you can work with the cookies of a site, you need to navigate to some of its pages.

Cookie cookie = new Cookie("key", "value");
this.driver.Manage().Cookies.AddCookie(cookie);

Get All Cookies

var cookies = this.driver.Manage().Cookies.AllCookies;
foreach (var currentCookie in cookies)
{
    Debug.WriteLine(currentCookie.Value);
}
this.driver.Manage().Cookies.DeleteCookieNamed("CookieName");

Delete All Cookies

this.driver.Manage().Cookies.DeleteAllCookies();
var myCookie = this.driver.Manage().Cookies.GetCookieNamed("CookieName");
Debug.WriteLine(myCookie.Value);

10. Maximize Window

Use the Maximize method of the IWindow interface.


public void MaximizeWindow()
{
    this.driver.Navigate().GoToUrl(@"http://automatetheplanet.com");
    this.driver.Manage().Window.Maximize();
}

Related Articles

Resources, Web Automation

Most Complete Selenium WebDriver C# Cheat Sheet

As you know, I am a big fan of Selenium WebDriver. You can find tonnes of useful code in my WebDriver Series. I lead automated testing courses and train people

Most Complete Selenium WebDriver C# Cheat Sheet

Web Automation

Selenium C# NUnit Test Automating Angular, React, VueJS and 20 More

In the new article from the Web Automation Series with C#, we will talk about creating a data-driven NUnit test automating all major web technologies such as Re

Selenium C# NUnit Test Automating Angular, React, VueJS and 20 More

Web Automation

Advanced Reuse Tactics for Grid Controls Automated Tests

In my previous articles Design Grid Control Automated Tests Part 1, Design Grid Control Automated Tests Part 2, Design Grid Control Automated Tests Part 3 I sta

Advanced Reuse Tactics for Grid Controls Automated Tests

Web Automation

Design Grid Control Automated Tests Part 3

In my previous articles Design Grid Control Automated Tests Part 1 and Design Grid Control Automated Tests Part 2 I started a mini-series about writing decent g

Design Grid Control Automated Tests Part 3

Web Automation

Design Grid Control Automated Tests Part 2

In my previous article Design Grid Control Automated Tests Part 1 I started this mini-series about writing decent grid control's automated tests. In this second

Design Grid Control Automated Tests Part 2

Web Automation

Advanced Web UI Components Automation with WebDriver C#

Most of the websites out there use commercial Web UI Components for their front end.  Most of these components are JavaScript-based, and their test automation i

Advanced Web UI Components Automation with WebDriver C#
Anton Angelov

About the author

Anton Angelov is Managing Director, Co-Founder, and Chief Test Automation Architect at Automate The Planet — a boutique consulting firm specializing in AI-augmented test automation strategy, implementation, and enablement. He is the creator of BELLATRIX, a cross-platform framework for web, mobile, desktop, and API testing, and the author of 8 bestselling books on test automation. A speaker at 60+ international conferences and researcher in AI-driven testing and LLM-based automation, he has been recognized as QA of the Decade and Webit Changemaker 2025.