I'm encountering some difficulties when it comes to selecting a dropdown in Protractor.
Here's the structure of my DOM:
https://i.stack.imgur.com/qK8sT.png
This is the XPath I'm using to select the dropdown with the value "Yes":
//label[contains(text(),"is your family safe?")]//parent::app-control-header//following-sibling::select//option[contains(text(),'Yes')
My approach to selecting the dropdown using the above XPath is as follows:
First, I created an XPath to retrieve the select block and stored it in a variable called dropDownBlock:
dropDownBlock = element(by.xpath('//label[contains(text(),"is your family safe?")]//parent::app-control-header//following-sibling::select'));
FamilysafeDropdown () {
selectDropdownByText(dropDownBlock, "yes");
}
I then created a function that accepts the element finder and a string, and selects the value based on the provided text:
public selectDropdownByText(dropdownElement: ElementFinder, text: string) {
dropdownElement.click();
dropdownElement.element(by.xpath('//option[contains(text(), "' + text + '")]')).click();
}
The issue I'm facing is that the code always finds the element with xpath
//option[contains(text(), "Yes")]
, and there are multiple dropdowns in my DOM with this same XPath. Therefore, I want to specifically select the value using the following XPath:
//label[contains(text(),"is your family safe?")]//parent::app-control-header//following-sibling::select//option[contains(text(),'Yes')
If anyone can provide guidance on what might be causing this problem, I would greatly appreciate it.