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

Time:11-07

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.

CodePudding user response:

  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));
    
  • Related