How to make protractor press the enter key?

2019-02-05 11:06发布

问题:

I've tried this:

browser.actions().keyDown(protractor.Key.ENTER).keyUp(protractor.Key.Enter).perform();

which gives the error:

Error: Not a modifier key

回答1:

Keyup/Keydown is limited to modifier keys in WebDriver (shift, ctrl, etc). I think you want

browser.actions().sendKeys(protractor.Key.ENTER).perform();



回答2:

The actions() is not required.

You can do something like:

var input = $('#someInput');
input.sendKeys(protractor.Key.ENTER);

Update: some people have complained that you are not sending the enter to browser. If you want to do this just change your selector:

$('body').sendKeys(protractor.Key.ENTER);


回答3:

Here is another way of doing this

var enter = browser.actions().sendKeys(protractor.Key.ENTER);
enter.perform();


回答4:

From the docs here...

http://appfigures.github.io/webdriver-js-api-reference/symbols/webdriver.WebElement.html#sendKeys

var myInput = element(by.model('myModel.inputName'));
myInput.sendKeys(value, protractor.Key.ENTER);

Also other examples from the docs linked above.

myInput.sendKeys("text was",
                 protractor.Key.CONTROL, "a", protractor.Key.NULL,
                 "now text is");
// Alternatively:
myInput.sendKeys("text was",
                 protractor.Key.chord(protractor.Key.CONTROL, "a"),
                 "now text is");