Tips for uploading a photo in Selenium Webdriver using Java when the input type is not 'File'

<span>
<span class="glyphicon glyphicon-plus avatarUploacIcon"></span>
Upload profile image</span>

Here is the code I am currently using:

 WebElement uploading=driver.findElement(By.cssSelector("div[class='form_advanced_wrapper my-events_host_wrapper'] span[class='glyphicon glyphicon-plus avatarUploacIcon']"));
         uploading.click();
         uploading.sendKeys("C:\pic.jpg");

I have been trying to upload an image but haven't been successful. Even after trying various methods, I have not been able to upload the image because my type is not 'File'. If anyone has successfully uploaded an image without using the File type, please share with me how you did it. Thank you!

Answer №1

    Finally, after some trial and error, I was able to figure out the solution when the Input Type is not "File"....
Appreciate all the helpful suggestions from everyone who guided me through this process.
        

public static void main(String[] args) throws InterruptedException
            {
                WebDriver driver = new FirefoxDriver();
                String appUrl = "http://demo.theupbeetkitchen.com/";
                driver.get(appUrl);
                driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
                driver.findElement(By.id("customLogin")).click();
                 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                 String newSet = driver.getWindowHandle();
                 driver.switchTo().window(newSet);   
                 driver.findElement(By.id("login_div")).click(); 
                 driver.findElement(By.id("ID")).sendKeys("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0469762a7765717665666c2a63717470653d36446369656d682a676b69">[email protected]</a>");
                 driver.findElement(By.id("Password")).sendKeys("123456");
                 driver.findElement(By.id("loginButton")).submit();
                 driver.findElement(By.id("customWelcome Chef Saurabh Gupta!")).click();
                 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                 driver.findElement(By.className("btn")).click();
                 driver.findElement(By.id("photoVideoPage")).click();
                 Thread.sleep(2000);
                 WebElement uploading=driver.findElement(By.cssSelector("div[class='form_advanced_wrapper my-events_host_wrapper'] span[class='glyphicon glyphicon-plus avatarUploacIcon']"));
                 uploading.click();
                 uploadFile("C:\\pic.jpg");
                 System.out.println("Successfully Done...........");
            }
            public static void uploadFile(String fileLocation) 
            {
                try
                {
                    setClipboardData(fileLocation);
                    Robot robot = new Robot();  
                    robot.keyPress(KeyEvent.VK_CONTROL);
                    robot.keyPress(KeyEvent.VK_V);
                    robot.keyRelease(KeyEvent.VK_V);
                    robot.keyRelease(KeyEvent.VK_CONTROL);
                    robot.keyPress(KeyEvent.VK_ENTER);
                    robot.keyRelease(KeyEvent.VK_ENTER);
                    robot.delay(2000);
                    robot.keyPress(KeyEvent.VK_ENTER);

                } 
                catch (Exception exp) 
                    {
                        exp.printStackTrace();
                    }
            }
            private static void setClipboardData(String string) 
            {
                // TODO Auto-generated method stub
                 StringSelection stringSelection = new StringSelection(string);
                   Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);

            }

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

Adding a timestamp to a video with varying date and time formats generated by an Android device

I am working on an android app to add a timestamp to a video using the FFMPEG library, and while it is functioning properly, it adds the timestamp using FFMPEG's default date time format. https://i.sstatic.net/Dq4y1.png It currently uses the YYYY-MM ...

Use JQuery to constantly monitor for any changes in HTML Div

I am dealing with a browser-based chat application where separate div elements are generated for each chat conversation. An external module oversees the entire chat process, and I do not have access to its underlying code. It is important to note that the ...

At the ready stance for a particular grade of students attending a class

I have created a page with a navigation menu that can be scrolled using the wheel mouse. My goal is to ensure that the li item with the 'selected' class is always positioned in the first position on the left. Is there a way to achieve this using ...

Hover over an element to trigger the background fading effect on the parent div

I'm looking to create a hover effect on an element within a fullscreen div. When I hover over the element, I want a background image to fade in on the div with the class ".section." While I can easily display the background image, I am unsure of how t ...

What is the weight of Java Monitors?

Imagine you have an array containing thousands of objects, and only a handful of threads that might access each object. The goal is to protect the access to one specific method within these objects. One way to achieve this would be by declaring the method ...

Parsing Json into Kotlin Data Class with GSON

I am working with a Java POJO class that looks like this: class Topic { @SerializedName("id") long id; @SerializedName("name") String name; } In addition, I also have a Kotlin data class structured as follows: data class Topic(val id: L ...

Can Angular apps be deployed onto the IBM Websphere Application Server?

At my workplace, we utilize IBM RAD for development and IBM Websphere Application Server as our web app server. Given these specific tools, I am curious about the feasibility of developing and deploying Angular SPAs. Perhaps integrating a Typescript plug ...

Ways to extract information from a website for presentation within an application

Looking for advice on how to integrate data from my website into an Android app. What is the most effective method to achieve this? Any examples or best practices? Would using a JSON file and retrieving it in my app be the ideal approach? Additionally, h ...

Unexpected Visual Basic Web Label Formatting

I am facing an issue with the styling of a complex label. The CSS code doesn't always apply as expected. Interestingly, when I ran this code on two different machines, the styles were inconsistent. The code seems messy and disorganized to me, but al ...

What are the best ways to engage with a div element using keyboard shortcuts?

Is it possible to enable keyboard shortcuts for interacting with div elements? I am working on a project tailored for seniors who may have difficulty using a mouse. Is there a way to utilize keyboard shortcuts to click on divs and access their contents? H ...

Having trouble starting Chrome with Selenium?

Having some trouble launching Chrome using Selenium Webdriver. I've made sure to set the correct path in the property, but unfortunately, I keep encountering this error message: "The path to the driver executable must be set by the webdriver.chrome.dr ...

Erase the dynamically loaded page using ajax and conceal the div

Currently, I am utilizing the .on() method with jQuery to display a specific div, and also using .load() to fetch a particular div from a web page hosted on my server. My query is how can I close this div when clicking outside of it, along with removing i ...

Present Images and Text Alongside Each Other in a Grid Layout

My goal is to have an image and text displayed side by side, but for some reason they are appearing stacked on top of each other. https://i.stack.imgur.com/F8OXL.png If you want to take a look at the code I am using, here is the link to my CodePen: https: ...

Issue arises with the Test class needing to have precisely one public zero-argument constructor

Having an issue with the constructor. Seeking some assistance please. The test is failing due to the constructor. public class ParameterTest extends MainTest { int number; boolean expectedPrime; @Parameterized.Parameters public static List<Integer ...

Expandable fluid inline svg that dynamically fills the remaining space alongside text within a flex row

How can I ensure that an inline SVG occupies all available space within a row without specifying its height across different screen sizes? The solution involves setting the SVG to have 100% height with a viewBox, allowing it to adjust accordingly while ma ...

Challenges with Java programming

Looking for assistance with resolving this problem. I am unsure about where to input the area number. Additionally, can someone explain what this expression means: in.nextInt(); Below is the code for the program: Create a Java program that takes in an ...

`The function `setTimeout` throws an error when passed as a string.`

Here are two pieces of code that may look identical, but one works properly: function hideElement(obj) {setTimeout(function() {obj.style.display = "none";}, 20);} On the other hand, the second one results in error: obj is not defined: function hideEleme ...

Issues with loading an external CSS file in an HTML document

I've been attempting to implement a loading animation using jQuery, but I'm encountering issues with loading the CSS file. I have tried various paths for the css file such as: <link href="css/animation.css" type="text/css" rel="stylesheet"> ...

Using Hibernate 4 Validator alongside Hibernate 3 validation annotations

Is there a way to make old validator annotations from the org.hibernate.validator package work with the hibernate 4 validator without having to refactor the entire project? Currently, the standardized Validator from the javax package, created as described ...

What is the process to create a sticky Material UI grid element?

Currently, I am in the process of developing the front-end for a shopping cart. To organize the content, I have split the container into two columns using Grid container and Grid item. In each column, various components are placed - the left side containin ...