Skip to content

Instantly share code, notes, and snippets.

@linktohack
Last active April 14, 2016 23:43
Show Gist options
  • Save linktohack/64596c9583012ce479b5306a0a1bd1e9 to your computer and use it in GitHub Desktop.
Save linktohack/64596c9583012ce479b5306a0a1bd1e9 to your computer and use it in GitHub Desktop.
selenium real click via xdotool
var Promise = require('bluebird'),
child_process = require('child_process')
exec = Promise.promisify(child_process.exec);
realClick(driver, "document.querySelectorAll('.srg > div:nth-child(3) > div:nth-child(1) > h3:nth-child(1) > a:nth-child(1)')[0]");
function realClick(driver, element) {
return driver.executeScript("\
document.myNamespace = {};\
document.myNamespace.mousePositionHandler = function (e) {\
document.myNamespace.clientX = e.clientX;\
document.myNamespace.clientY = e.clientY;\
document.removeEventListener('mousemove', document.myNamespace.mousePositionHandler);\
};\
document.addEventListener('mousemove', document.myNamespace.mousePositionHandler);")
.then(function () {
return driver.executeScript("\
return [\
window.screenX + Math.round(window.outerWidth / 2),\
window.screenY + Math.round(window.outerHeight / 2)\
];");
}).then(function(val) {
return exec('xdotool mousemove --sync ' + val[0] + ' ' + val[1]);
}).then(function () {
return exec('xdotool mousemove_relative --sync 1 1');
}).then(function() {
return driver.executeScript("\
var uploadRect = " + element + ".getBoundingClientRect();\
return [\
uploadRect.left + 10 - document.myNamespace.clientX,\
uploadRect.top + 4 - document.myNamespace.clientY,\
];\
");
}).then(function(val) {
return exec('xdotool mousemove_relative --sync -- ' + val[0] + ' ' + val[1])
}).then(function () {
return exec('xdotool click 1');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment