Currently, I am working on automating a scenario where a user searches for an item and once the search results are displayed, they click on the serving size and quantity of that specific item.
https://i.sstatic.net/GV02V.jpg
However, I keep encountering an "element not found" exception when attempting to manipulate the text field by modifying the value and selecting the quantity size. Even after trying different locators for the div tag containing these elements (textbox, dropdown menu, buttons), the issue persists. I have confirmed that these elements are not within a frame. I have also attempted injecting JavaScript code to perform the operations, but with no success. Could someone please help me identify what I might be doing wrong?
<div id="loaded_item">
<input id="food_entry_food_id" type="hidden" name="food_entry[food_id]" value="348096022">
<input id="food_entry_date" type="hidden" value="2017-01-10" name="food_entry[date]">
<p class="food-description">Bananas, raw</p>
<div class="verified-food-container" style="display: block;">
<div class="user_submitted"></div>
<p></p>
<h3 class="secondary-title">How much?</h3>
<input id="food_entry_quantity" class="text short" type="text" size="30" name="food_entry[quantity]" autocomplete="off" value="1.0">
servings of
<select id="food_entry_weight_id" class="select" name="food_entry[weight_id]">
<p></p>
<p></p>
<h3 class="secondary-title">To which meal?</h3>
<select id="food_entry_meal_id" class="select" name="food_entry[meal_id]">
<p></p>
<input id="update_servings" class="button log" type="submit" value="Add Food To Diary" data-food-id="348096022" data-external-id="63109870792493" data-version-id="197946116099837">
<div class="nutritional_info">
</div>
Below is the snippet of my code used to locate these elements.
@FindBy(xpath=".//*[@id='searchFoodByName']/form/p/input[4]")
WebElement searchFoodDatabaseButton;
@FindBy(linkText = "Bananas, raw")
WebElement matchedFoodThirdOption;
@FindBy(xpath="//div[@id='loaded_item']/input[@id='update_servings']")
WebElement addFoodToDiary;
@FindBy(xpath=".//*[@id='date_controls']/form/span/a[2]")
WebElement clickOnNextDate;
@FindBy(xpath=".//*[@id='food_entry_quantity']")
WebElement foodEntryQuantity;
@FindBy(css = "select[id='food_entry_weight_id']")
WebElement foodEntryWeight;
@FindBy(css = "select[id='food_entry_meal_id']")
WebElement addFoodToMeal;
public void searchFoodItem() throws Exception{
TextBoxHelper.set(driver,searchFoodDatabase,"Banana");
ButtonHelpers.click(driver, searchFoodDatabaseButton);
ButtonHelpers.click(driver, matchedFoodThirdOption);
Utils.waitForElement(driver, By.xpath(".//*[@id='loaded_item']"), 2000);
foodEntryQuantity.clear();
foodEntryQuantity.sendKeys("2.0");
Select WeightPickerDropDown = new Select(foodEntryWeight);
WeightPickerDropDown.selectByIndex(2);
Select MealPickerDropDown = new Select(addFoodToMeal);
MealPickerDropDown.selectByIndex(2);
ButtonHelpers.jsClick(driver, addFoodToDiary);
System.out.println("after clicking the add button");
}