gpt4 book ai didi

javascript - 从无序列表中选择一个值 - 错误 "elementIdAttribute is not a function"

转载 作者:行者123 更新时间:2023-12-03 02:46:01 25 4
gpt4 key购买 nike

我在从下拉列表中访问值时遇到困难。该列表是使用 Bootstrap 的 dropdown.js 创建的。

我在“Nightwatch”中创建了一个带有 2 个输入参数的自定义命令:下拉按钮的“定位器”以及要在列表中找到的值。输出应从下拉列表中选择值。

问题:下拉菜单打开,但尝试从中访问值会生成错误:

TypeError: this.elementIdAttribute is not a function

我已经阅读了有关堆栈溢出的所有帖子,并且基于它们,语法是正确的。我没主意了。

注释下是下拉元素的路径,更改li:nth-child(2)的值我可以选择列表中的每个元素。

下面是代码(用***包裹的是产生错误的地方):

exports.command = function(locator, valueToClick) {
var dropdown;
//locator sample
//.click('#form > div.panel.panel-primary > div.panel-body > div:nth-child(1) > div > div.btn-group.bootstrap-select.form-control.parsley-validated > button')
//.click('#form > div.panel.panel-primary > div.panel-body > div:nth-child(1) > div > div.btn-group.bootstrap-select.form-control.parsley-validated.open > div > ul > li:nth-child(2) > a > span.text')
// cut " > button" from the end
var dropdownElementLocator = locator.slice(0,locator.length-9);
//var openDrop = '#form > div.panel.panel-primary > div.panel-body > div:nth-child(1) > div > div.btn-group.bootstrap-select.form-control.parsley-validated.open > div > ul li a span.text'
var openDrop = dropdownElementLocator + '.open > div > ul > li'

this
.click(locator)
.elements('css selector', openDrop, function(result){
console.log("number of elements = " + result.value.length);
console.log(result);
result.value.forEach(function (element){
console.log(***this.elementIdAttribute***(element.ELEMENT, 'a').value);
});

var position = -1;
var i = 0;
result.value.forEach(function(value){
i++;
var loc = openDrop + ':nth-child(' + i + ') > a > span.text'
})

position = position>-1 ? position : 0;
console.log("final position " + i )
//add ".open > div > ul > li:nth-child(2) > a > span.text"
dropdownElementLocator = dropdownElementLocator + ".open > div > ul > li:nth-child(" + (position+1) + ") > a > span.text";
this.click(dropdownElementLocator);
})
};

最佳答案

通过保留 this 的值消除了错误。因为 this 的值在整个代码中都会发生变化。

exports.command = function(locator, valueToClick) {
var dropdown;
var openDrop = dropdownElementLocator + '.open > div > ul > li'

// preserve the value of this since its scope changes trhoughout the code
browser = this;
browser
.click(locator)
.elements('css selector', openDrop, function(result){
console.log("number of elements = " + result.value.length);
console.log(result);
result.value.forEach(function (element){
console.log(browser.elementIdAttribute(element.ELEMENT, 'a').value);
});

var position = -1;
var i = 0;
result.value.forEach(function(value){
i++;
var loc = openDrop + ':nth-child(' + i + ') > a > span.text'
//this.element('css selector', loc, function(res) {
// console.log('value ' + i + ': ' + res)
//})
})

position = position>-1 ? position : 0;
console.log("final position " + i )
//add ".open > div > ul > li:nth-child(2) > a > span.text"
dropdownElementLocator = dropdownElementLocator + ".open > div > ul > li:nth-child(" + (position+1) + ") > a > span.text";
browser.click(dropdownElementLocator);
})
};

问题:我仍然无法访问下拉列表中的值

关于javascript - 从无序列表中选择一个值 - 错误 "elementIdAttribute is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48086864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com