I am trying to automate dropdown list in selenium web driver using Page object model. Below is my code explanation:

Viewed 17
// Language Selection
    public static void SelectLanguage() {
        waitForElementToBeClickable(driver.findElement(By.xpath("//div[@class=\"lang-identifier\"]")));
        driver.findElement(By.xpath("//div[@class=\"lang-identifier\"]")).click();
        List<WebElement> elements = driver.findElements(By.xpath("//ul[@class=\"dropdown-menu pull-right\"]/li"));
        for (WebElement e : elements) {
            String text = e.getAttribute("value");
            System.out.println(e.getText());
            if (text.equalsIgnoreCase("English")) {
                e.click();
                break;
            } else if (e.getText().equalsIgnoreCase("Español")) {
                e.click();
                break;
            } else if (e.getText().equalsIgnoreCase("Italiano")) {
                e.click();
                break;
            } else if (e.getText().equalsIgnoreCase("Pусский")) {
                e.click();
                break;
            } else if (e.getText().equalsIgnoreCase("Français")) {
                e.click();
                break;
            } else if (e.getText().equalsIgnoreCase("Português")) {
                e.click();
                break;
            } else {
                System.out.println("Please select appropriate language");
            }
        }

    }
1 Answers

I would suggest a much simpler but more flexible version of your method.

Some suggestions:

  1. I would change your method to take the desired language as a parameter to significantly simplify the code but also make it very flexible.

  2. WebDriverWait, in most cases, will return the found element(s). Use that to simplify your code to a one-liner, e.g.

    new WebDriverWait(...).until(ExpectedConditions.elementToBeClickable).click();
    

    You didn't provide the code of your custom method, waitForElementToBeClickable, but if you really want to keep it, have it return the element(s) waited for to make it more useful and save having to write extra code.

  3. If you have nested double quotes, I would suggest you use a combination of double and single quotes. It's a personal preference but I think it makes it easier to read than \", e.g.

    "//div[@class=\"lang-identifier\"]"
    

    would turn into

    "//div[@class='lang-identifier']"
    
  4. Instead of grabbing all options and then looping through them to compare the contained text to some desired string, use an XPath that contains the desired text instead, e.g. for "English" the XPath will look like

    //ul[@class='dropdown-menu pull-right']/li[text()='English']
    
  5. NOTE: .getAttribute("value") gets the value of an INPUT and will not work on other elements, e.g. the LI elements in your elements variable. .getText() returns the text contained in an element but will not work on INPUTs.

After implementing these suggestions, the code turns into a two-liner and is very flexible.

public static void SelectLanguage(String language) {
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.lang-identifier"))).click();
    driver.findElement(By.xpath("//ul[@class='dropdown-menu pull-right']/li[text()='" + language + "']")).click();
}
Related