【问题标题】:Press Enter Key in Selenium RC with C#使用 C# 在 Selenium RC 中按 Enter 键
【发布时间】:2012-05-01 20:34:48
【问题描述】:

如何使用 C# 使用 Selenium RC 按 Enter 键?

我正在使用SearchBox 使用Selenium。 我必须在其中输入一些名称,并且必须按 Enter 进行搜索。

没有Submit 按钮。所以,我必须使用 Enter

我试过这样的

selenium.KeyPress("quicksearchtextcriteria", "13");

但不起作用。

请帮忙。

注意:我收集了一些可能的方法来做到这一点。见这里:press enter key in selenium

【问题讨论】:

  • 只是一个旁注,你为什么不使用 selenium 2?

标签: c# selenium selenium-rc keypress enter


【解决方案1】:

这可以通过KeysEnter来实现。

Java 中的示例,因为我不知道 C#:

import org.openqa.selenium.Keys;

//...

// this sends an Enter to the element
selenium.type("locator", Keys.ENTER);

// or even this - this sends the "Any text" and then confirms it with Enter
selenium.type("locator", "Any text" + Keys.ENTER);

来自Keys 枚举。

【讨论】:

  • 导入 org.openqa.selenium.Keys;本身给出错误。我正在使用 Visual Studio。 c#
  • 哦,对不起,我的错。那是 Java,我没有从 sn-p 中认出 C#。 C#中没有替代方案吗?也许不是,我不知道。
  • c# 替代的链接不起作用。是否有 c# 替代品 - {code} selenium.type("locator", "Any text" + Keys.ENTER); {代码}
  • 错误链接的错误消息是:在此服务器上找不到请求的 URL /svn/trunk/docs/api/dotnet/html/T_OpenQA_Selenium_Keys.htm。这就是我们所知道的。
  • 我修复了链接。请注意,这个答案现在已经 6 岁了,而 Selenium RC 已经消失了。使用 WebDriver!
【解决方案2】:

试试这个:

导入 org.openqa.selenium.Keys

WebElement.sendKeys(Keys.RETURN)

参考资料:

Typing Enter/Return key in Selenium

http://asynchrony.blogspot.com/2008/11/enter-key-press-in-selenium.html

希望这会有所帮助。

【讨论】:

  • 当我尝试此操作时,我在 Visual Studio 2017 中收到以下错误:“参数 1 无法从 'System.Windows.Forms.Keys' 转换为 'string' - API SendKeys 需要一个字符串。
【解决方案3】:

这是用 C# 完成的:

webElement.SendKeys(Keys.Return);

【讨论】:

    【解决方案4】:

    我相信您也可以使用“提交”方法。 (虽然我使用的是 Selenium 2,所以我猜这在 Selenium RC 中可能是不可能的?抱歉,如果是这样)。

    //First you need to find the searchBox and fill it, once doing so call
    searchBox.Submit();
    

    【讨论】:

    • 或者:searchBox.SendKeys("要搜索的文本" + OpenQA.Selenium.Keys.Enter);
    【解决方案5】:

    试试这个:

    selenium.KeyPressNative("13");
    

    【讨论】:

      【解决方案6】:

      用这个

      selenium.keyDown("locator of element", "\\13");
      

      【讨论】:

      • 这里的元素定位器是什么意思?是我的文本框的 xpath 的意思吗?
      • 是的,只要放文本框的xpath
      • 嗯。我试过了。但没有用。没有错误。但是 Enter 不起作用
      • 当我调试它时,它说表达式已评估但没有值。
      • quicksearchtextcriteria 是字段的 ID???如果是,那么尝试使用定位器作为 ID=quicksearchtextcriteria...我认为这是 xpath 错误消息
      【解决方案7】:

      您可以使用 clickAt("locator","");如果按键不工作。它肯定会起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 2012-04-19
        • 2015-06-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多