How to click a button in the drop-down menu using selenium in my java app?

I need to read data from a website and build menu objects based on this data.
But before I can do this, I need to use the English version of the site and click on the language change button in the upper right corner. The problem is that the language change button is not always clicked and the following exception occurs: “org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:”css selector”,”selector”:”.lang-list .lang-list-item:nth-child(2)”}
Here is my code:

  1. The method with which the problem occurs because the button is not always pressed:
    private void changeLanguage(WebDriver driver) {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
        WebElement languageMenu = wait.until(ExpectedConditions.elementToBeClickable(By.className("menu-switcher")));
        languageMenu.click();
        wait.until(ExpectedConditions.presenceOfElementLocated((By.cssSelector(".lang-list .lang-list-item:nth-child(2)"))));
        WebElement enLanguage = driver.findElement(By.cssSelector(".lang-list .lang-list-item:nth-child(2)"));
        enLanguage.click();
    }
  1. Next, you find methods for building menus, getting links to all categories, and building categories:
private Menu crawlExpirenza(WebDriver driver, String url) {
        driver.get(url);
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
        WebElement info = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("info-wrapper")));
        WebElement placeName = info.findElement(By.className("title"));
        Menu.MenuBuilder menuBuilder = Menu.builder();
        menuBuilder.placeName(placeName.getText());

        Map<String, String> allCategoriesLinks = getAllCategories(driver, wait);
        List<Category> categories = new ArrayList<>();
        for (String link : allCategoriesLinks.keySet()) {
            Category category = buildExpirenzaCategory(link, driver, wait, allCategoriesLinks );
            categories.add(category);
        }
        menuBuilder.categories(categories);
        return  menuBuilder.build();
    }




    private Map<String, String> getAllCategories(WebDriver driver, WebDriverWait wait) {
        Map<String, String> allCategories = new HashMap<>();
        **changeLanguage(driver);**
        List<WebElement> sideMenuCategories = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.className("category__link")));

        for (WebElement sideMenuCategory : sideMenuCategories) {
            String href = sideMenuCategory.getAttribute("href");
            String name = sideMenuCategory.getText();
            allCategories.put(href, name);
        }
        return allCategories;
    }

    private Category buildExpirenzaCategory(String link, WebDriver driver, WebDriverWait wait, Map<String,String> allCategories) {
        driver.navigate().to(link);
        **changeLanguage(driver);**
        Category.CategoryBuilder categoryBuilder = Category.builder();
        categoryBuilder.name(allCategories.get(link));
        List<Dish> dishes = buildExpirenzaDishes(wait);
        categoryBuilder.dishes(dishes);
        return categoryBuilder.build();
    }

Please help me to fix the changeLanguage method so that the button on the link is clicked correctly and also so that it works for each category (Cheesecakes and other desserts, Bakery, Coffee and other drinks, Lemonades, tonics and others, Sparkling based cocktails, Organic and non-alcohol wine, Beer, Gifts and something else – All these categories have different links)

I tried using other web drivers and the mobile version of the site, but the button is still clicked only half the time.

New contributor

Natalia Shtykh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật