Skip to content
Advertisement

How do I select option from a Select Listbox dynamically during runtime ? Selenium_Java

I have a select listBox of around 10 elements containing account no. with balance. These balances change sometimes they go to zero some time they have some amount, what I want to do is select the option at runtime which has a positive account balance. The format of the text in LB is : Acc no. (INR 12345 :SomeText)

Thanks in advance.

Advertisement

Answer

  1. load select option

  2. get select option xpath

  3. then get all the text reside in the option element

  4. Then you logic

      List<WebElement> allOptions = driver.findElements(optionXpath);
      List<String> optionHasMoreMoney = new ArrayList<String>();
    
            for(WebElement eachEle: allOptions){
                long amount = Long.parseLong(eachEle.getText());
                if(amount > 0){
                    optionHasMoreMoney.add(eachEle.getText());
                }
            }
    
    
            // list of options which has more money
            Select select = new Select("your select element");
            select.selectByValue(optionHasMoreMoney.get(0));
    
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement