在带有 Ruby 的 Selenium WebDriver 中按 TAB 然后按 ENTER 键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10040953/
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
Press TAB and then ENTER key in Selenium WebDriver with Ruby
提问by AJJ
I am doing automated testing using Selenium WebDriver with Ruby. I need to click a button. I cannot get the button element by id or css or xpath as the button is transparent. I would like to use Tab and Enter key to press the button.
我正在使用 Selenium WebDriver 和 Ruby 进行自动化测试。我需要点击一个按钮。我无法通过 id 或 css 或 xpath 获取按钮元素,因为按钮是透明的。我想使用 Tab 和 Enter 键按下按钮。
I can use Tab key to get the button as below:
我可以使用 Tab 键来获取按钮,如下所示:
@element.send_keys :tab
@element --> any javascript element visible in the browser
But how do I use the Enter key on the button?
但是如何使用按钮上的 Enter 键呢?
Basically I need to achieve press Tab key and then press Enter key to click the button.
基本上我需要实现按 Tab 键然后按 Enter 键单击按钮。
I am using Selenium WebDriver @driver = Selenium::WebDriver.for :firefox
我正在使用 Selenium WebDriver @driver = Selenium::WebDriver.for :firefox
回答by ShockwaveNN
In Ruby user1316's code looks like
在 Ruby user1316 的代码看起来像
driver.action.send_keys(elementVisible, :tab).send_keys(elementVisible, :return).perform
回答by Ziran
Keeping in mind the excerpt :
记住摘录:
I can use tab key to get the button as
@element.send_keys :tab
@element --> any javascript element visible in the browser
but how do i use the enter key on the button??
我可以使用 tab 键来获取按钮
@element.send_keys :tab
@element --> 浏览器中可见的任何 javascript 元素
但是我如何使用按钮上的回车键??
In order to use the enter key on the button, you could try one of the solution provided using Ruby here. This basically talks about sending the :returnvalue and not the :entervalue i.e @element.send_keys :returnand some additional information.
为了使用按钮上的回车键,您可以尝试使用 Ruby here提供的解决方案之一。这基本上是关于发送:return值而不是:enter值 ie @element.send_keys :return和一些附加信息。
UPDATED:
更新:
I could provide some code in Java which tries to implement the problem conceptually using the info provided here. You could try to translate for the corresponding Ruby Selenium API.
我可以提供一些 Java 代码,这些代码尝试使用此处提供的信息在概念上实现问题。您可以尝试翻译相应的 Ruby Selenium API。
The Code:
编码:
Actions builder = new Actions(driver);
builder.sendKeys( elementVisible, Keys.TAB).sendKeys(Keys.RETURN);
Action submitTheTransperentButton = builder.build();
submitTheTransperentButton.perform();
动作生成器 = 新动作(驱动程序);
builder.sendKeys( elementVisible, Keys.TAB).sendKeys(Keys.RETURN);
行动 submitTheTransperentButton = builder.build();
submitTheTransperentButton.perform();
回答by AlekseiPetrovski
send ENTER in ruby:
用 ruby 发送 ENTER:
@browser.action.send_keys("\n").perform

