如何在用 C# 编写的 Selenium WebDriver(Nunit 测试用例)中按“Enter”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17552477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:49:46  来源:igfitidea点击:

How to press "Enter" in Selenium WebDriver (Nunit Test Case) written in C#?

c#nunitselenium-webdriverenter

提问by RON12345

I am trying to create a automation framework with nunit + Selenium + c#

我正在尝试使用 nunit + Selenium + c# 创建一个自动化框架

Our webadmin is based on Devexpress framework hence I can't click button by it's "ID" or atleast I dont know how to. The subtitute to this is simply pressing "Enter" button. I have already tried

我们的网络管理员基于 Devexpress 框架,因此我无法通过它的“ID”点击按钮,或者至少我不知道如何点击。对此的替代只是按“Enter”按钮。我已经试过了

driver.FindElement(By.XPath("String")).SendKeys(Keys.Enter);

回答by Akbar

RON, there is a possibility that DOM is taking time to load after the GoToUrl call. Increase the implicit wait time so that findElement waits more time before throwing any exception. Or use explicit wiat --- http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp

RON,在调用 GoToUrl 之后,DOM 可能需要一些时间来加载。增加隐式等待时间,以便 findElement 在抛出任何异常之前等待更多时间。或者使用显式 wiat --- http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp

If still this doesnt work then use Actions class -- http://www.guru99.com/keyboard-mouse-events-files-webdriver.html

如果这仍然不起作用,则使用 Actions 类 -- http://www.guru99.com/keyboard-mouse-events-files-webdriver.html

回答by user2330678

Use below code to click on an invisible button.

使用下面的代码点击一个不可见的按钮。

    IWebElement tmpElement = Driver.FindElement(By.Id("invisibleButton"));
    var executor = (IJavaScriptExecutor)Driver;
    executor.ExecuteScript("arguments[0].click();", tmpElement);
    wait.Until(d => { return d.Title.Equals("pageTitle"); });

回答by MushtaqAR

using OpenQA.Selenium.Interactions;

Actions builder = new Actions(driver);        
builder.SendKeys(Keys.Enter);

For more information: Typing Enter/Return key in Selenium

有关更多信息:在 Selenium 中键入 Enter/Return 键