使用打字稿处理量角器下拉列表的问题

Issue in handling dropdown in protractor using typescript

我在 select 量角器

的下拉菜单中遇到问题

我的Dom看起来像这样

这是 select 值为 Yes

的下拉菜单的 XPath
//label[contains(text(),"is your family safe?")]//parent::app-control-header//following-sibling::select//option[contains(text(),'Yes')]

以下是我尝试 select 使用上面的 XPath

下拉列表的方法

首先,我创建了一个 XPath,它检索 select 块并存储在 dropDownBlock 中,如下所示

dropDownBlock = element(by.xpath('//label[contains(text(),"is your family safe?")]//parent::app-control-header//following-sibling::select'));

FamilysafeDropdown () {        
    selectDropdownByText(dropDownBlock, "yes");
}

我创建了一个接受元素查找器和字符串的函数,select根据传递的字符串输入值

public selectDropdownByText(dropdownElement: ElementFinder, text: string) {
    dropdownElement.click();
    dropdownElement.element(by.xpath('//option[contains(text(), "' + text + '")]')).click();
}

我的问题是代码总是用 xpath 找到元素

//option[contains(text(), "Yes")]" 并且在我的 DOM 中有多个使用此 XPath 的下拉菜单。

所以我想 select 使用 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 + '")],这里的前缀.表示从dropdownElement.

开始搜索HTML个元素

没有.,表示从HTML页开始搜索HTML元素。

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