1

Is there any way to use nightmare (the nodeJS module) to press an arrow key? Either by sending the key to the specific element or by clicking on the element and sending the keypress to the page as a whole?

My problem is that there is a dropdown menu which, when clicked on, displays some options below it. These options do not appear to be clickable, I have tried click and even the realClick nightmare extension. Neither seem able of selecting any element of the dropdown. While playing around with the page, I found that by using the arrow keys and the enter key, I could select the options without clicking. So is there any way to send the keypress events to electron? Either through nightmare or by accessing electron directly?

Wso
  • 192
  • 2
  • 11
  • 1
    You might be talking specifically about – devilpreet Jun 23 '17 at 16:19
  • @devilpreet I tried to use element.setSelectedIndex, but it had the same result as clicking, nothing happens, no visible change on the webpage. Thank you for the idea though! – Wso Jun 23 '17 at 17:23
  • 1
    My bad. It was element.selectedIndex. Please refer: [Select](https://developer.mozilla.org/en/docs/Web/API/HTMLSelectElement). I have worked on detection of such elements and believe me, using HTML API is the only best bet to work across. Only way I see it not happening is having custom handlers, however I would like to see such example. Thanks – devilpreet Jun 24 '17 at 05:32
  • I tried that as well, no luck. Thank you for the updated suggestion though, it is much appreciated. I believe the problem is that the element appears to just be a div with a ul inside it, and not a select element. – Wso Jun 26 '17 at 16:44
  • Hmm. You can try [keydown](https://developer.mozilla.org/en/docs/Web/Events/keydown) using evaluate. And it is important which element you are targeting. Also, sometimes it may need multiple events (which looks like single event) as in [this case] (https://stackoverflow.com/questions/36175940/clicking-an-image-with-nightmarejs/44725340#44725340). If you can share the website or reproduce it, I can try giving it a shot. – devilpreet Jun 27 '17 at 04:18
  • While I can't give the actual site out, I can say that if you go to any search engine and type in some text, that will reproduce what I need to do. I need to type something, hit the down arrow key, and then hit enter to get the first autocompleted result. – Wso Jun 29 '17 at 14:55

1 Answers1

1

It is called auto complete menu and there is no standard way of doing it. I tried working with Google and came up with a method. I think, it will be tough to make generic solution, as it depends on how auto complete is implemented. Hope this helps!

var Nightmare = require('nightmare');

var nightmare = Nightmare({
  show: true,
  webPreferences: {}
})

nightmare
  .goto('http://www.google.co.in')
  .evaluate(function(searchTerm) {
    var elem = document.querySelector('#lst-ib');
    elem.value = searchTerm;

    //Do mousedown to initiate auto complete window
    var event = document.createEvent('MouseEvent');
    event.initEvent('mousedown', true, true);
    elem.dispatchEvent(event);
  }, 'Leo')
  .then(function() {
    //Wait for results to appear
    nightmare.wait(1000)
    .evaluate(function() {
      //Click the first option of List
      var first = document.querySelector('#sbtc > div.gstl_0.sbdd_a > div:nth-child(2) > div.sbdd_b > div > ul > li:nth-child(1)');
      first.firstElementChild.click();
    }).then(function() {
      console.log('All Done');
    });
  });
devilpreet
  • 749
  • 4
  • 18
  • In case of duckduckgo it is first firing click to get auto complete window. And second event is also click on first option. Basically, check the event listeners on the element(text input and autocomplete option) concerned which would give you fair idea on which event should be fired. – devilpreet Jun 30 '17 at 04:01
  • I was able to get it working by selecting rather than using the down arrow key. Although this is more of a work around than a solution, it did work. Thank you for your suggestion and your example code! – Wso Jul 05 '17 at 17:21
  • This does not answer the question =(, @Wso have you found a way to truly simulate a key press? – adelriosantiago Aug 26 '18 at 06:42