相关疑难解决方法(0)

了解Selenium中的执行异步脚本

我一直在使用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)

或者,获取所有元素属性字典(python):

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()

问题是硒特异性,但与语言无关.

javascript python selenium selenium-webdriver protractor

23
推荐指数
2
解决办法
2万
查看次数