Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 267
God_Father Avatar asked Dec 22 '13 16:12

God_Father


People also ask

How do you use Ctrl +C in Selenium?

Copy & Paste Text: When we need to copy some text from one text box to another, we select the text by pressing "CTRL+A" they copy the text using "CTRL+C" and paste the text in the new text box by simply clicking in the text box and pressing keys "CTRL+V".

How do you press keys such as Alt SHIFT and CTRL in Selenium?

Press SHIFT - using keyDown. Press Ctrl - using keyDown. Press then release S (this key can be pressed and immediately released using sendKeys method)


2 Answers

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.

like image 162
Vikas Ojha Avatar answered Sep 29 '22 07:09

Vikas Ojha


Maybe this one will help:

 System.Windows.Forms.SendKeys.SendWait("{ESC}");
like image 38
Nat Avatar answered Sep 29 '22 09:09

Nat