我一直在使用selenium
(使用python 绑定和protractor
大部分时间)相当长的时间,每次我需要执行 javascript 代码时,我都使用了execute_script()
方法。例如,滚动页面(python):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
var div = element(by.css('div.table-scroll'));
var lastRow = element(by.css('table#myid tr:last-of-type'));
browser.executeScript("return arguments[0].offsetTop;", lastRow.getWebElement()).then(function (offset) {
browser.executeScript('arguments[0].scrollTop = arguments[1];', div.getWebElement(), offset).then(function() {
// assertions
});
});
driver.execute_script('var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;', element)
但是,WebDriver API 也有execute_async_script()
我个人没有使用过的。
它涵盖哪些用例?我什么时候应该使用execute_async_script()
而不是常规的execute_script()
?
这个问题是特定于selenium的,但与语言无关。