如何使用C#在Selenium WebDriver中按'Esc'键

God*_*her 6 c# automation keypress selenium-webdriver

我有一种情况,我必须按'ESC'键才能停止加载页面..

这肯定是必需的,否则页面将继续加载一分钟.

如何使用selenium webdriver按"Esc"键.这必须使用C#完成.

另外,请提及必须导入的类

Vik*_*jha 8

您可以使用Actions类将密钥直接发送到浏览器.请参阅以下代码的最后两行:

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);
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.

  • 这通过在 SendKeys 之后调用 Perform 方法对我有用,如下所示:action.SendKeys(OpenQA.Selenium.Keys.Escape).Perform(); (4认同)