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

不幸的是,我试图测试的表单(不是我自己的代码,所以我不能修改)没有Submit按钮。当手动使用它时,我只需输入Enter或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()

其他回答

也可以使用Action界面来实现。在WebDriver的情况下-

WebElement username = driver.findElement(By.name("q"));
username.sendKeys(searchKey);
Actions action = new Actions(driver);
action.sendKeys(Keys.RETURN);
action.perform();
Actions action = new Actions(driver);
action.sendKeys(Keys.RETURN);

如果你是在这种情况下:

a)只是想按下键,但你没有一个特定的webElement来点击

b)您正在使用Selenium 2 (WebDriver)

那么解决方案是:

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

要使用Selenium输入键,首先需要导入以下库:

import org.openqa.selenium.Keys

然后在想要输入密钥的地方添加此代码

WebElement.sendKeys(Keys.RETURN);

您可以根据需要将RETURN替换为列表中的任何键。