admin管理员组

文章数量:1305152

I'm testing a site (in Chrome) that has a "spotlight search" feature like on Macs, where an input shows up when you press a certain key. There is a directive that takes care of the logic for this, and you can type anywhere in the application, as long as it's not an input. I need to test this feature by sending keys through my protractor tests, but I get unknown error: cannot focus element when I target a div. Is it possible to send keys on an element that isn't an input?

I'm testing a site (in Chrome) that has a "spotlight search" feature like on Macs, where an input shows up when you press a certain key. There is a directive that takes care of the logic for this, and you can type anywhere in the application, as long as it's not an input. I need to test this feature by sending keys through my protractor tests, but I get unknown error: cannot focus element when I target a div. Is it possible to send keys on an element that isn't an input?

Share Improve this question edited Jul 28, 2015 at 23:41 alecxe 474k127 gold badges1.1k silver badges1.2k bronze badges asked Jul 28, 2015 at 22:28 cocoacocoa 3,9217 gold badges31 silver badges57 bronze badges 2
  • Possibly related: stackoverflow./questions/21991003/… – steve v Commented Jul 28, 2015 at 22:36
  • I am already using browser.ignoreSynchronization and browser.sleep(5000) as that answer suggests, still have the same problem – cocoa Commented Jul 28, 2015 at 22:44
Add a ment  | 

1 Answer 1

Reset to default 8

You would probably need to wait for the element to bee visible with a visibilityOf Expected Condition and browser.wait():

var EC = protractor.ExpectedConditions;
browser.wait(EC.visibilityOf(by.css("div#myid")), 5000);

var div = element(by.css("div#myid"));
div.sendKeys("test");

Also, you can try utilizing browser.actions():

browser.actions().mouseMove(div).sendKeys('test').perform();

Also, you may need to click the element before sending keys:

div.click().sendKeys("test");

本文标签: javascriptSend keys on divnon inputStack Overflow