相关疑难解决方法(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万
查看次数

xpath 查找伪元素 ::after 在没有任何内容的 div 元素中

我正在尝试编写 xpath 以查找选中或未选中的复选框,正在使用 css ::after 元素更改此复选框。以下是我拥有的两个元素

//div[@class='FormBlock-formItem2' and .//text()='Scoped In']//div[@class='FormBlock-controlIndicator']
Run Code Online (Sandbox Code Playgroud)

复选框的两个元素

我需要发现没有选择“Scoped Out”并且选择了“Scoped In”。我看到的唯一区别是复选框第二部分中的“::after”。我尝试了 xpath :" //div[@class='FormBlock-formItem2' and .//text()='Scoped In']//div[@class='FormBlock-controlIndicator'] "但这是找到'Scoped In' 元素,但我无法验证它是否被选中。挣扎了几天。请帮忙。

selenium xpath selenium-webdriver

4
推荐指数
1
解决办法
1万
查看次数