Dealing with file upload dialog using Selenium web automation

I am having difficulty managing the 'select files to load' dialog using Selenium WebDriver. Here is the HTML code snippet:

<form class="upload">
    <button class="btn" data-capture="" type="button">Browse</button>
    <input class="hide" type="file" accept="..." multiple="" data-file-input=""/>
</form>

My goal is to:

  1. Remove the 'hide' class;
  2. Send keys with file path after the element is visible;
  3. Hide the element again.

This is the code I have written to achieve this:

JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("$('.hide:not(.layout)').removeClass('hide')");
(webDriver.findElement(By.cssSelector(".upload>input"))).sendKeys("path_to_file");
js.executeScript("$('.hide:not(.layout)').addClass('hide')");

However, I encountered an exception on the 3rd line:

"Runtime.evaluate threw exception: TypeError: Cannot read property 'click' of null"

What could be causing this issue?

Answer №1

Appreciate it, user1433852! It appears that the issue was indeed related to the wait function. The solution was simply adding a wait for ajax, which resolved the problem successfully!

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

Initiate a series of tasks and await their completion using RxJS / Redux Observables

My app requires an initialisation action to be fired, followed by a series of other actions before triggering another action. I found a similar question on Stack Overflow However, when attempting this approach, only the initial APP_INIT action is executed ...

Are there any tools available that can convert ThreeJS code into WebGL?

Are there any existing tools that can convert ThreeJS to WebGL? Or, could you provide guidance on creating a converter for ThreeJS to WebGL? ...

There are two distinct varieties of object arrays

I recently encountered an issue while trying to sort my array of Star Wars episodes by episode ID. The problem emerged when comparing two arrays: one manually inputted in the code snippet labeled as "1" on the screenshot, and the other generated dynamicall ...

Locate identical values within an array in MongoDB, even if they exist independently of an object

{ "_id" : ObjectId("15672"), "userName" : "4567", "library" : [ { "serialNumber" : "Book_1" }, { "serialNumber" : "Book_2" }, { "serialNumber" : "Book_4" } ...

Retrieve the data from the file input field

I am attempting to dynamically change the value of an input type text based on the input type file selection. Despite my efforts with the code below, I have been unable to find a solution to this issue. HTML: <input type="text" name="imagem" class="fi ...

Exploring the process of iterating through a JSON post response and displaying its contents within image divs

After receiving a JSON post response, I am looking to iterate over the data and display it in image divs. Can anyone provide guidance on how to achieve this using JavaScript? Thank you. Here is the JavaScript code that handles the JSON post response: cor ...

Python script unable to locate div popup using Selenium

Recently, I've been working on enhancing my selenium skills, but I'm facing a challenge in finding a solution to this particular issue. While experimenting with the functionality of the page located at: www.omegle.com When using selenium to cli ...

The scroll animation feature was not functioning properly in Next.js, however, it was working flawlessly in create react app

I recently transitioned a small project from Create React App (CRA) to Next.js. Everything is working as expected except for the scroll animations in Next.js, which are not functioning properly. There are no errors thrown; the animations simply do not occ ...

Unstyled Cards Failing to Receive Design

I am currently working on creating a prototype that utilizes two Bootstrap 4 cards to display information from a form and store related information from another form in the second card. The current layout of this setup can be observed below: This JSFiddle ...

Fade out the notification div using jQuery in MVC4

I'm a beginner in the world of JavaScript and JQuery and I could really use some assistance with resolving a simple issue that I've encountered. As part of my application's functionality, I am dynamically loading the following div based on ...

Enhance Your Website with GatsbyJS Custom Scrollbars

I'm struggling to customize the default scrollbar on my Gatsby website with an overlay scrollbar. I want to avoid the page 'shifting' slightly to the left when switching between pages that have scrollbars and those that do not. My attempt i ...

Having issues with json_decode not functioning correctly after using JSON stringify

After encoding a JavaScript array into JSON and posting it to PHP, I encountered an issue. Before posting the data, when I checked a value in the array using console.log(selection[878][2824]), I received the expected result. However, after encoding the var ...

"I've noticed that my Python Selenium program tends to automatically shut down after running for 2 to 3 hours. It appears to be a WebSocket Connection issue

Currently, I am in the process of developing an automation program using Python Selenium. The objective is to have the program running continuously around the clock, 24 hours a day. However, I have encountered an issue where the program unexpectedly term ...

Omit the usage of java.lang.* within the Clojure namespace

Is it feasible to omit the class names from java.lang in a Clojure namespace? I want to utilize variables such as Byte and String, but java.lang class names are causing hindrance. perhaps something along the lines of (ns my-ns (:exclude java.lang)) ? ...

Are you looking for straightforward dynamic directives that come with dynamic controllers and a scope?

Feeling like I have a simple problem to solve here. Working within the confines of a TypeScript + Angular application. Within a controller, I've got an array of similar directives that I want to utilize. These are essentially the panels strewn throug ...

Hide the dropdown menu when the user clicks anywhere else on the screen

I have a scenario with 2 dropdown buttons. When I click outside the dropdown or on it, it closes. However, if I click on the other dropdown button, it does not close and the other one opens. I want them to close when I click on the other button or anywhere ...

Sending information from one page to another and then sending it once more

I am currently utilizing the following code to submit a form's data to : <script type="text/javascript"> $(document).ready(function(){ $("#textnextofkin").validate({ debug: false, rules: { ...

Tips for utilizing an event listener for a submission button

I am encountering a problem with this event listener. I attempted to add an event listener to the submit button, but it seems to be inactive. I can't figure out what mistake I have made! Here is my code: const form = document.getElementById("form") ...

Is Selenium Service not recognizing the executable_path parameter?

Currently using Selenium 4.1.5, I encountered an issue while trying to execute the following code: from selenium import webdriver from selenium.webdriver.chrome.service import Service service = Service(executable_path=f'./chromedriver.exe') driv ...

Mastering Inter-Composable Communication in Vue 3: A Guide

Composables in Vue documentation demonstrate how small composition functions can be used for organizing code by composing the app. Discover More About Extracting Composables for Code Organization "Extracted composables act as component-scoped servi ...