首页 > 解决方案 > 使用打字稿处理量角器下拉列表的问题

问题描述

我在量角器中选择下拉菜单时遇到问题

我的 Dom 看起来像这样

下拉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')]

我不明白这里的问题,有人可以以正确的方式指出我。

标签: javascripttypescriptxpathprotractorui-automation

解决方案


问题来自以下代码行中使用的 xpath:

dropdownElement.element(by.xpath('//option[contains(text(), "' + text + '")]')).click();

你应该使用.//option[contains(text(), "' + text + '")],这里的前缀.意味着搜索 HTML 元素从dropdownElement.

如果没有.,则表示搜索 HTML 元素从 HTML 页面的开头开始。

在 XPath 中,.表示当前节点。


推荐阅读