【问题标题】:How to press "Enter" in Selenium WebDriver (Nunit Test Case) written in C#?如何在用 C# 编写的 Selenium WebDriver(Nunit 测试用例)中按“Enter”?
【发布时间】:2013-07-07 07:27:21
【问题描述】:

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

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

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

【问题讨论】:

  • 那么为什么.Click 不起作用?
  • 我收到此错误。单击“ClassLibrary2.UnitTest1.TestMethod2: OpenQA.Selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互”
  • 您可以在查询中包含按钮的 html 吗??
  • 'at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebDriver.cs:OpenQA 的第 1009 行。 Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebDriver.cs:line 849 at ClassLibrary2.UnitTest1.TestMethod2() in C :\Users\Administrator\documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 46 '
  • 我的代码看起来像这样... [Test] public void TestMethod2() { driver.Navigate().GoToUrl("localhost/websearch"); driver.FindElement(By.Id("pcLogin_Panel1_txtUsername_I" )).SendKeys("auto test1"); driver.FindElement(By.Id("pcLogin_Panel1_txtPassword_I")).SendKeys("Exchange1"); driver.SendKeys(Keys.Enter).perform; driver.FindElement(By.Id ("pcLogin_Panel1_btnLogon_B")).Click(); Assert.AreEqual("Web Admin - Dashboard", driver.Title); }

标签: c# nunit selenium-webdriver enter


【解决方案1】:
using OpenQA.Selenium.Interactions;

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

欲了解更多信息:Typing Enter/Return key in Selenium

【讨论】:

    【解决方案2】:

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

     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"); });
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        Keys.Return()解决了按enter的问题

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-05
          • 2012-08-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多