我一直在使用selenium(使用python绑定并且通过protractor大部分)很长一段时间,每次我需要执行javascript代码时,我都使用了execute_script()方法.例如,对于滚动页面(python):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
Run Code Online (Sandbox Code Playgroud)
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
});
});
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
但是,WebDriver API也有execute_async_script()我个人没有使用过的.
它涵盖了哪些用例?我什么时候应该使用execute_async_script()而不是常规execute_script()?
问题是硒特异性,但与语言无关.
当应该使用SetScriptTimeout时,请提供任何示例.
我知道这个定义
Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.
Run Code Online (Sandbox Code Playgroud)
但不确定它究竟做了什么.