How to simulate ctrl-click or shift-click with webdriver.io?

混江龙づ霸主 提交于 2019-12-05 10:29:20

Edit: If you want to select different elements using ctrl key:

client.elements(<css selector for your list of elements>, function(err, res) {
    client
         .moveTo(res.value[<index of element you want to select>].ELEMENT, 0, 0)
         .keys('Ctrl') #every action after this within the scope of `client.elements` will have the `ctrl` key depressed
         .buttonPress('left')
         .moveTo(res.value[<index of element you want to select>].ELEMENT, 0, 0)
         .buttonPress('left')
         .moveTo(res.value[<index of element you want to select>].ELEMENT, 0, 0)
         .buttonPress('left')
         #repeat `.moveTo` and `.buttonPress` for every element you want to `ctrl` select
         .keys('NULL'); #This command or `.keys('Shift') will release the `shift` key.
});

To select using the shift key you use the code below (assuming you want to select every item in your list of elements -- obviously you can change the indexes to get a specific subsection of your list of elements). It will move to the top left of the first element in your list of elements, then left click, then hit the shift key, then move to the top left of the last element, left click again, and then release the shift key:

client.elements(<css selector for your list of elements>, function(err, res) {
    client
         .moveTo(res.value[0].ELEMENT, 0, 0)
         .buttonPress('left')
         .keys('Shift')
         .moveTo(res.value[(res.value.length-1)].ELEMENT, 0, 0)
         .buttonPress('left')
         .keys('NULL'); #This command or `.keys('Shift') will release the `shift` key.
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!