Selenium webdriver was not able to find the specified div element

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");

            }

Answer №1

Your implementation of JavascriptExecutor appears to have an error.

((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('value', '2.0')", WebElement);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Foundation Unveil Modal hidden from view

I'm currently integrating modals using Foundation 5 in a Rails application. The issue I'm facing is that the modal only works when the page is not scrolled down. If you scroll to the bottom of the page and try to activate the modal by clicking ...

Activate jQuery after a vanilla JavaScript function has been executed

I have created a jQuery function that captures any changes made to an input field and stores them in a variable. The input field is connected to a vanilla JavaScript based API library, which I am unable to convert to jQuery. This library is for an address ...

Warning: Unhandled Promise Rejection - Alert: Unhandled Promise Rejection Detected - Attention: Deprecation Notice

Encountering the following error message: (node:18420) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of undefined at C:\Users\ohrid\Desktop\backend2\routes\categories.js:27:24 at Layer.han ...

Tips for using jQuery dropdown menus

I am currently in the process of creating a prototype for a quick dropdown menu using jQuery. However, I have reached the limits of my knowledge on jQuery and could use some assistance in solving my issue. My objective is to have a dropdown animate down w ...

How can I showcase array elements using checkboxes in an Ionic framework?

Having a simple issue where I am fetching data from firebase into an array list and need to display it with checkboxes. Can you assist me in this? The 'tasks' array fetched from firebase is available, just looking to show it within checkboxes. Th ...

Issue with triggering the change event for <select> tag

Whenever the selected value of the drop down changes, the following code does not work as expected. Please make corrections if any errors are present. <!doctype html> <html lang="en"> <head> <meta charset="utf-8</scri ...

Unable to transfer bootstrap form data from the view to the controller due to issues with bootstrap validations

Scenario I have integrated a bootstrap form into my codeigniter application with bootstrap validation to ensure data accuracy. I want the submit button to work properly so that users can successfully submit the form and be redirected to the action page ...

Issue with background image resizing when touched on mobile Chrome due to background-size property set to cover

My current challenge involves setting a background image for a mobile page using a new image specifically designed for mobile devices. The image is set with the property background-size: cover, and it works perfectly on all platforms except for mobile Chro ...

Encountering difficulties obtaining server response while utilizing dropzone.js version 4

I am currently using dropzone.js version 4 to facilitate file uploads from a webpage to my server. While the upload process is functioning properly, I am encountering difficulty in retrieving the server response. It should be noted that I am creating the D ...

How do I retrieve the id of an event using a class descriptor within a JQuery Dialog constructor in Javascript?

As someone who is relatively new to JavaScript and jQuery, I must apologize in advance if my question seems basic. :) In a straightforward webpage, I am utilizing JQuery UI's Tabs and attempting to implement a double-click feature on the Tab names th ...

The international telephone input library's "grunt img" command is malfunctioning

I am currently utilizing intl-tel-input to develop a control for displaying mobile numbers. Since the flags are quite small, I am in need of enlarging them. A reference for this can be found here: https://codepen.io/jackocnr/full/ONXWgQ To achieve this, ...

Error: X does not conform to function definition

I'm currently in the process of developing a Vue application that interacts with Spotify's API to search for tracks. However, I've encountered an issue where I receive the following error message: Uncaught (in promise) TypeError: searchTra ...

Hover over the div to center an SVG inside it

Simply put, I am working on a design where a gradient bar moves above a specific element when hovered, creating a visual effect of a triangle. I am now looking for a way to center an SVG inside the element on hover, to create a similar triangular gradient ...

Issue with TypeORM @BeforeInsert causing a field in Entity not to be populated with value

Currently, I am facing an issue where I am attempting to update or insert into a token field before the record is saved. However, when utilizing the @BeforeInsert hook, I encounter the following error: "error": "Cannot read property 'co ...

Sending handlebars variable to the client-side JavaScript file

I am currently working on an application using node.js along with express and handlebars, and I'm trying to find a way to transfer handlebars data from the server to client-side JavaScript files. Here is an example: //server.js var person = { na ...

What is the best way to import modules in Typescript/Javascript synchronously during runtime?

I have a Typescript class where I am attempting to perform a synchronous import, however, the import is being executed asynchronously. My code snippet looks like this: --------------100 lines of code-------------------- import('../../../x/y/z') ...

Issue with registering click events when using phantomjsdriver and selenium-support

I am a beginner with the PhantomJSDriver and I'm trying to input my email, then proceed to the next screen by clicking the "NEXT" button on https://www.youtube.com/content_id. Although I can successfully input my email, the click event doesn't s ...

When passing context to createPage for use in a query, there may be issues if a string is inadvertently converted to a number, causing the query to fail

I am facing an issue with a page creation function that I have implemented. Here is the code snippet: createPage({ context: { productId: String(productId) }, Despite explicitly converting the ID to a string, the page template is still receiving it as a ...

Develop a custom directive that incorporates ng-model and features its own distinct scope

UPDATE - I have generated a Plunker I am in the process of developing a personalized directive to be utilized for all input fields. Each input will have distinct options based on the logged-in user's requirements (mandatory, concealed, etc), so I bel ...

Cloud Function scheduler incorporating dynamic memory allocation control

I am currently working on setting up a scheduled function in Firebase Cloud Functions that interacts with third-party APIs. However, I am facing an issue with the memory limit being exceeded due to the large size of the data processed by this function. Al ...