driver.findElement(By by)
and driver.findElements(By by)
scopes the while DOM.
you can target a small area of the DOM with:
element.findElement(By by)
and element.findElements(By by)
Using this:
WebElement dropdown = driver.findElement(By.DROPDOWN LOCATOR);
List<WebElement> options = dropdown.findElements(By.OPTION LOCATOR);
You still need the OPTION LOCATOR. But it is easier to achieve, it will collect child elements from the parrent element only.
Select methods would look like this:
public void selectByText(List<EebElement> options, String text) {
for (WebElement element: options) {
if (element.getText().equals(text)) {
element.click();
break;
}
}
}
public void selectByValue(List<EebElement> options, String value) {
for (WebElement element: options) {
if (element.getAttribute("value").equals(value)) {
element.click();
break;
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…