Home > Software design >  Java/selenium : list.clear(); clearing the result that i was trying to store in map before clearing
Java/selenium : list.clear(); clearing the result that i was trying to store in map before clearing

Time:09-17

I Was trying to store country and state values in map with key-value pair every time I loop through one country and get the list of states, I am trying to store them in Map and clearing the list upon selecting other countries but list.clear(); clearing the result that I have already stored in the map as well. please guide me here.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class DropdownTest {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "D:\\Java\\Automation\\drivers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://frontendscript.com/demo/country-dropdown-list-javascript/");
        
        WebElement country = driver.findElement(By.id("country"));
        Select country_sel = new Select(country);
        List<WebElement> countries = country_sel.getOptions();
        
        WebElement state = driver.findElement(By.id("state"));
        Select state_sel = new Select(state);
        
        Map<String, List<String>> result = new HashMap<String, List<String>>();
        
        List<String> state_names = new ArrayList<String>();
        for (WebElement c : countries) {
            if(!(c.getText().equalsIgnoreCase("Select Country"))) {
                
            c.click();
            
            List<WebElement> states = state_sel.getOptions();
        
            for (WebElement s : states) {
                if(!(s.getText().equalsIgnoreCase("Select State"))) {
                    //System.out.println(s.getText());
                    state_names.add(s.getText());
                }
            }
            //System.out.println(state_names);
            result.put(c.getText(), state_names);
            state_names.clear();
            System.out.println(result);
        }}
    }
}

CodePudding user response:

When you put the list as value to map and clear list later, it actually refer to same object. So clearing the list will impact the element in map value as well.

So the correct way to do it is create List<String> state_names inside the for loop.

Updated Code:

        Map<String, List<String>> result = new HashMap<String, List<String>>();

        for (WebElement c : countries) {
            if(!(c.getText().equalsIgnoreCase("Select Country"))) {
                
            List<String> state_names = new ArrayList<String>(); // Inside the loop
            
            c.click();
            
            List<WebElement> states = state_sel.getOptions();
        
            for (WebElement s : states) {
                if(!(s.getText().equalsIgnoreCase("Select State"))) {
                    state_names.add(s.getText());
                }
            }
            result.put(c.getText(), state_names);
            System.out.println(result);
            
        }}

CodePudding user response:

@bhuvan Your code looks good for me. can you share us the html code with us? it will be easy for us to suggest.

I usually get the list of text by following code,

public List<String> constructList(By by){
    
    List<WebElement> wholelistEle = driver.findElements(by);
    List<String> wholelist = new ArrayList<>();
    wholelistEle.forEach(ele -> {
        if(!ele.getText().equals("")){
            wholelist.add(ele.getText());
        }
    });
    log.info("Whole values found are : " wholelist.toString());
    return wholelist;
}

construct your xpath of select option and pass to this method when you iterating countries loop,

like

By optionXpath = By.xpath("//select//option");

List<String> stateList = constructList(optionXpath);

CodePudding user response:

You need to define all the state option once you are in for each loop for country. Also before going inside the inner for each loop, you should clear the state list.

Code:

driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
driver.get("https://frontendscript.com/demo/country-dropdown-list-javascript/");
WebElement country = driver.findElement(By.id("country"));
//WebElement state = driver.findElement(By.id("state"));
Select country_sel = new Select(country);
List<WebElement> countries = country_sel.getOptions();
Map<String, List<String>> result = new HashMap<String, List<String>>();
List<String> state_names = new ArrayList<String>();
for (WebElement c : countries) {
    if(!c.getText().equalsIgnoreCase("Select Country")) {
        c.click();
        List<WebElement> states = new Select(wait.until(ExpectedConditions.presenceOfElementLocated(By.id("state")))).getOptions();
        state_names.clear();
        for (WebElement s : states) {
            if(!s.getText().equalsIgnoreCase("Select State")) {
                //System.out.println("State name name " s.getText());
                state_names.add(s.getText());
            }
        }
        //System.out.println("Country name " c.getText());  
        //System.out.println("State name"   state_names);

        result.put(c.getText(), state_names);
    }
}
System.out.println(result);
}

Output :

{Papua New Guinea=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Cambodia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Wales=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Kazakhstan=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Paraguay=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Syria=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Bahamas=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Solomon Islands=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Montserrat=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Mali=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Marshall Islands=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Pitcaim Islands=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Guadeloupe=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Panama=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Laos=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Argentina=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Virgin Islands=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Seychelles=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Belize=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Zambia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Bahrain=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Guinea-Bissau=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Macedonia, Former Yugoslav Republic of=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Namibia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Comoros=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Faroe Islands=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Finland=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Netherlands Antilles=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Georgia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Saint Kitts and Nevis=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Yemen=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Eritrea=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Puerto Rico=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Holy See (Vatican City)=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Aruba=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Madagascar=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Libya=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Romainia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Sweden=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], West Bank=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Cocos (Keeling) Islands=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Malawi=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Liechtenstein=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Poland=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Svalbard=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Bulgaria=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Jordan=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Tunisia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Gambia, The=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Tuvalu=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], United Arab Emirates=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Kenya=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], French Polynesia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Brunei=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Djibouti=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Lebanon=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Azerbaijan=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Cuba=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Mauritania=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Saint Lucia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Guernsey=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Man, Isle of=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Mayotte=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Israel=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], San Marino=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Australia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Tajikistan=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Cameroon=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Gibraltar=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Cyprus=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Northern Mariana Islands=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Malaysia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Iceland=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Oman=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Armenia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Gabon=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Toga=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Yugoslavia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Trinidad=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Luxembourg=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Brazil=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Algeria=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Jersey=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Slovenia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Antigua and Barbuda=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Colombia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Ecuador=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Moldova=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Vanuatu=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Honduras=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Italy=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Nauru=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Korea, North=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Haiti=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Afghanistan=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Burundi=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Singapore=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], French Guiana=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], American Samoa=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Christmas Island=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Scotland=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Russia=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Netherlands=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], Clipperton Island=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands], China=[Bulawayo, Harare, ManicalandMashonaland Central, Mashonaland East, Mashonaland West, Masvingo, Matabeleland North, Matabeleland South, Midlands],....}

There are more output but stackoverflow does not allow to post body more than 30000 chars.

  • Related