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:
- 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();
}
- 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.
Natalia Shtykh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.