Skip to content

Instantly share code, notes, and snippets.

@dmjcomdem
Created August 1, 2018 12:27
Show Gist options
  • Save dmjcomdem/b82515423086c3d5110041015faf499c to your computer and use it in GitHub Desktop.
Save dmjcomdem/b82515423086c3d5110041015faf499c to your computer and use it in GitHub Desktop.
convert Xpath to CSS
function xPathToCss(xpath) {
return xpath
.replace(/\[(\d+?)\]/g, function(s,m1){ return '['+(m1-1)+']'; })
.replace(/\/{2}/g, '')
.replace(/\/+/g, ' > ')
.replace(/@/g, '')
.replace(/\[(\d+)\]/g, ':eq($1)')
.replace(/^\s+/, '');
}
xPathToCss('[@id="userjsGrid"]/div[1]/table/tbody/tr[2]/td[6]/input')
// => [id="userjsGrid"] > div:eq(0) > table > tbody > tr:eq(1) > td:eq(5) > input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment