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

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


当前回答

对于使用XPath的Selenium WebDriver(如果键是可见的):

driver.findElement(By.xpath("xpath of text field")).sendKeys(Keys.ENTER);

or,

driver.findElement(By.xpath("xpath of text field")).sendKeys(Keys.RETURN);

其他回答

selenium.keyPress("css=input.tagit-input.ui-autocomplete-input", "13");
Actions action = new Actions(driver);
action.sendKeys(Keys.RETURN);

如果你只是想按Enter键(python):

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

action = ActionChains(driver)
action.send_keys(Keys.ENTER)
action.perform()

我必须在文本中间输入回车键。于是我通过下面的文本发送键函数来实现1\n2\n3:

1\N{U+E007}2\N{U+E007}3

您可以在输入文本的元素对象上调用submit()。

或者,你也可以将Enter键发送给它,如下面的Python代码片段所示:

from selenium.webdriver.common.keys import Keys
element.send_keys(Keys.ENTER) # 'element' is the WebElement object corresponding to the input field on the page