简体   繁体   中英

Putting a longer javascript into executeScript

Wanting to get the position/coordinates of an element and then trigger the click via the position/coordinates. Im planning to do it in javascript via the executeScript but realised it a bit long. Is there a better way to do some concatenation rather than my one liner. Reason im using javascript is because im more familiar in that then the selenium/nodejs way

driver.executeScript("var elem = document.querySelector('.mob-menu-icon');var leftPosition = elem.getBoundingClientRect().left;var topPosition = elem.getBoundingClientRect().top;document.elementFromPoint(leftPosition, topPosition).click();");

driver.executeScript() accept String or Function, so you can pass a function:

driver.executeScript(function(){
  var elem = document.querySelector('.mob-menu-icon');
  var leftPosition = elem.getBoundingClientRect().left;
  var topPosition = elem.getBoundingClientRect().top;
  document.elementFromPoint(leftPosition, topPosition).click();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM