1

I am trying to enter a keyword to an auto-select box and select the specific text that I'm passing from the populated list.

Keyword: text_customer
Text needed to select: Constants.INBOUNDORDER_CUSTOMER

So far, the keyword is written in the auto-complete text box and the list is populated. Currently, I'm unable to get the list of elements as I'm struggling to find a unique element. ID is dynamic.

WebElement select = driver.findElement(text_customer);
            List<WebElement> options = select.findElements(By.cssSelector("div[class*= 'mat-autocomplete-panel ng-star-inserted mat-autocomplete-visible']"));
            for(WebElement option1: options){
            if(Constants.INBOUNDORDER_CUSTOMER.equals(option1.getText().trim()))
                option1.click();
                break;
            }

enter image description here

2 Answers 2

1

To select the element with text as Constants.INBOUNDORDER_CUSTOMER as the element is a Angular element you need to induce WebDriverWait for the elementToBeClickable() and you can use the following Locator Strategies:

List<WebElement> options = select.findElements(By.xpath("//div[@class= 'mat-autocomplete-panel ng-star-inserted mat-autocomplete-visible']//mat-option[contains(@class, 'mat-option')]"));
for(WebElement option: options){
    if (option1.getText().contains("INBOUNDORDER_CUSTOMER"))
        option1.click();
        break;
}
2
  • 1
    Thank you so much! This worked for me :)
    – dly
    Commented Jan 27, 2021 at 14:41
  • 1
    Sorry for the delay. All good now.
    – dly
    Commented Jan 27, 2021 at 15:23
1

To gather the items for your list it looks like you don't need to be specific. Try something like this:

List<WebElement> options = driver.findElements(By.xpath(//*[contains(@id,"mat-option-")]));

That will populate your options List with all the elements that have an ID that contains mat-option-

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.