如何使用Cypress/JavaScript从选择中调用所有选项值?
我正在尝试在下获取所有
,以便有一个数组。 这是我的代码:value
选择标签
cy.get('[data-tab-name-prefix="selectedFonds[0]]')
.scrollIntoView()
.should('be.visible')
.find('[name="selectedFonds[0][name]"]')
.find('option')
.invoke('attr', 'value').then($options => {
cy.log($options)
})
<div data-tab-name-prefix="selectedFonds[0]">
<div class= something>
<div class= something else>
<select class=form-control name="selectedFonds[0][name]">
<option value="first value"> First Value </option>
<option value="second value"> Second Value </option>
<option value="third value"> Third Value </option>
<option value="forth value"> Forth Value </option>
</select>
</div>
</div>
</div>
问题在于它仅返回第一个选项...并非全部。
i am trying to get all values
under select tag
in order to have an array.
This is my code:
cy.get('[data-tab-name-prefix="selectedFonds[0]]')
.scrollIntoView()
.should('be.visible')
.find('[name="selectedFonds[0][name]"]')
.find('option')
.invoke('attr', 'value').then($options => {
cy.log($options)
})
<div data-tab-name-prefix="selectedFonds[0]">
<div class= something>
<div class= something else>
<select class=form-control name="selectedFonds[0][name]">
<option value="first value"> First Value </option>
<option value="second value"> Second Value </option>
<option value="third value"> Third Value </option>
<option value="forth value"> Forth Value </option>
</select>
</div>
</div>
</div>
The problem is that it returns only the first option... not all of them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.invoke('attr','value')
正在引起问题,它将选项的数组更改为单个值。将
attr()
移动then()
中。.invoke('attr', 'value')
is causing the problem, it changes the array of options to a single value.Move the
attr()
call inside the.then()
.我的建议是用数据属性来定位元素,然后在()中执行
。然后定位选择元素,然后做
children()
。然后是每个()
块,以迭代每个选项元素。My suggestion would be to target the element with the data attribute then do
within()
. Then target the select element and dochildren()
. Followed by aeach()
block to iterate over every option element.