我正在寻找一种快速的方法来键入进入或返回键在硒。

不幸的是,我试图测试的表单(不是我自己的代码,所以我不能修改)没有Submit按钮。当手动使用它时,我只需输入Enter或Return。我怎么能做到这一点与硒类型命令,因为没有按钮点击?


当前回答

Actions action = new Actions(driver);
action.sendKeys(Keys.RETURN);

其他回答

Actions action = new Actions(driver);
action.sendKeys(Keys.RETURN);

你只需要这样做:

final private WebElement input = driver.findElement(By.id("myId"));
input.clear();
input.sendKeys(value); // The value we want to set to input
input.sendKeys(Keys.RETURN);

Java

driver.findElement(By.id("Value")).sendKeys(Keys.RETURN);

OR,

driver.findElement(By.id("Value")).sendKeys(Keys.ENTER);

Python

from selenium.webdriver.common.keys import Keys
driver.find_element_by_name("Value").send_keys(Keys.RETURN)

OR,

driver.find_element_by_name("Value").send_keys(Keys.ENTER)

OR,

element = driver.find_element_by_id("Value")
element.send_keys("keysToSend")
element.submit()

Ruby

element = @driver.find_element(:name, "value")
element.send_keys "keysToSend"
element.submit

OR,

element = @driver.find_element(:name, "value")
element.send_keys "keysToSend"
element.send_keys:return

OR,

@driver.action.send_keys(:enter).perform
@driver.action.send_keys(:return).perform

C#

driver.FindElement(By.Id("Value")).SendKeys(Keys.Return);

OR,

driver.FindElement(By.Id("Value")).SendKeys(Keys.Enter);

有以下几种按键方式- c#:

Driver.FindElement(By.Id("Value")).SendKeys(Keys.Return);

OR

OpenQA.Selenium.Interactions.Actions action = new OpenQA.Selenium.Interactions.Actions(Driver);
action.SendKeys(OpenQA.Selenium.Keys.Escape);

OR

IWebElement body = GlobalDriver.FindElement(By.TagName("body"));
body.SendKeys(Keys.Escape);

如果你正在寻找“如何在Selenium WebDriver (Java)中从键盘按Enter键”,那么下面的代码一定会帮助你。

// Assign a keyboard object
Keyboard keyboard = ((HasInputDevices) driver).getKeyboard();

// Enter a key
keyboard.pressKey(Keys.ENTER);