3

I want to select value by text from a dynamic non select dropdown. I did some research and I found this code:

WebElement element = driver.findElement(ByMapper.getBy(dropdown));
element.click();
List<WebElement> options = element.findElements(By.xpath("/html/body/div[1]/div[2]/div/div/div"));
for (WebElement option : options){
    if (option.getText().contains(text)){
        option.click();
        break;
    }
}

Basically it put the dropdowns options into a List element, and run through in a for loop, and if the options contains text, click it and break the loop. However it is not working with this type of dropdown:

Snapshot:

Dropdown

Can you suggest what can I do?

Snapshot of Specific value:

Specific value

Note: The page is only available via private vpn, so I cannot share it.

2
  • Can you share page URL? Commented Feb 18, 2022 at 10:08
  • This is only available via private vpn Commented Feb 18, 2022 at 10:14

1 Answer 1

1

To select the value by text Teszt_5 from a dynamic non Select dropdown you can use the following locator strategies:

String text = "Teszt_5";
WebElement element = driver.findElement(ByMapper.getBy(dropdown));
element.click();
List<WebElement> options = element.findElements(By.xpath("//div[@class='mat-autocomplete-panel mat-autocomplete-visible ng-star-inserted' and starts-with(@aria-labelledby, 'mat-form-field-label')]//mat-option//span[@class='mat-option-text']"));
for (WebElement option : options){
    if (option.getText().contains(text)){
        option.click();
        break;
    }
}

References

You can find a couple of relevant detailed discussions in:

Sign up to request clarification or add additional context in comments.

1 Comment

is this Angular? sure you can not use [ngModel] to know the value and the text of the option selected?. I know that we can use javascript in an Angular app, but really is necesary?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.