javascript - 使用打字稿处理量角器下拉列表的问题
问题描述
我在量角器中选择下拉菜单时遇到问题
我的 Dom 看起来像这样
这是选择带有值 Yes 的下拉列表的 XPath
//label[contains(text(),"is your family safe?")]//parent::app-control-header//following-sibling::select//option[contains(text(),'Yes')]
以下是我尝试使用上面的 XPath 选择下拉列表的方式
首先,我创建了一个 XPath,它检索选择块并存储在 dropDownBlock 中,如下所示
dropDownBlock = element(by.xpath('//label[contains(text(),"is your family safe?")]//parent::app-control-header//following-sibling::select'));
FamilysafeDropdown () {
selectDropdownByText(dropDownBlock, "yes");
}
我创建了一个函数,它接受元素查找器和字符串,并根据传递的字符串选择值
public selectDropdownByText(dropdownElement: ElementFinder, text: string) {
dropdownElement.click();
dropdownElement.element(by.xpath('//option[contains(text(), "' + text + '")]')).click();
}
我的问题是代码总是用 xpath 找到元素
//option[contains(text(), "Yes")]"
并且我的 DOM 中有多个带有此 XPath 的下拉列表。
所以我想用 XPath 选择值
//label[contains(text(),"is your family safe?")]//parent::app-control-header//following-sibling::select//option[contains(text(),'Yes')]
我不明白这里的问题,有人可以以正确的方式指出我。
解决方案
问题来自以下代码行中使用的 xpath:
dropdownElement.element(by.xpath('//option[contains(text(), "' + text + '")]')).click();
你应该使用.//option[contains(text(), "' + text + '")]
,这里的前缀.
意味着搜索 HTML 元素从dropdownElement
.
如果没有.
,则表示搜索 HTML 元素从 HTML 页面的开头开始。
在 XPath 中,.
表示当前节点。
推荐阅读
- c++ - std::vector 和 std::array 之间的区别
- .net - Ms Graph 读取消息给出 ErrorAccesDenied
- ios - 如何使用原始 iPhone SE 尺寸的 SwiftUI 画布
- arrays - Reactjs 删除数组的特定索引
- powershell - 收到乱序的 PowerShell 事件
- sql - ORA-02291: SQL Developer 中的完整性约束错误消息
- sql - 按状态分组的社区计数
- wpf - 我需要
.config 文件以在 WPF 中正常运行 - winapi - Windows 10 API 例程的汇编程序调用约定
- python - 如何从 Pandas DF 创建(正确)一个 NumPy 数组