How to press 'Esc' key in Selenium WebDriver using C#

automation, c#, keypress, selenium-webdriver

Solution

You can send keys directly to the browser using Actions class. See last two lines of the following code:

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;

IWebDriver driver = new FirefoxDriver(ffprofile);
driver.Navigate().GoToUrl("http://www.google.com");
driver.FindElement(By.Name("q")).SendKeys("Stack Overflow");
driver.FindElement(By.Name("q")).Submit();

Actions action = new Actions(driver);
action.SendKeys(OpenQA.Selenium.Keys.Escape);

Hope that helps.

Problem

I have a situation where I have to press on the 'ESC' key to stop the page from loading.. This is definitely needed as otherwise the page will keep on loading for a minute. How do I make selenium webdriver to press the 'Esc' key. This has to be done using C#. Also, kindly mention the class that has to be imported

Original source