How to upload an Image using selenium webdriver?

Answered

Hi All,
I am facing the issue while uploading an image into website using selenium. I tried with the simplest solution  which we identify the locator of the upload button and pass the Path of the file.But it is not working for me as the attribute type=button not type=file..
Is there any workaround for this.. Please let me know if anyone has the solution other than using AutoIT.

HTML snippet is :
<input id=”dzuploadbttn class=”pink-button type=”button name=”dzuploadbttn value=”Upload style=”display: block;>

here is the site
https://www.designerexchange.com/       —–>click on Sell To US  —>

I did not find the click on Sell To US, please let us know the page as well on which we will get this button.

on November 5, 2014.
Add Comment
  • 4 Answer(s)
    Best answer

    Hi Ravinder,

    Try this:

    
    package testCases;
    import java.awt.AWTException;
    
    import java.awt.Robot;
    
    import java.awt.event.KeyEvent;
    
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    
    import org.openqa.selenium.WebDriver;
    
    import org.openqa.selenium.firefox.FirefoxDriver;
    public class Example {
    
    public static WebDriver driver;
    
    public static void main(String[] args) throws InterruptedException, AWTException {
    
    driver = new FirefoxDriver();
    
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    driver.get("https://www.designerexchange.com/index.php/sell/quote");
    
    driver.manage().window().maximize();
    
    Thread.sleep(3000);
    driver.findElement(By.id("uploadmorelnk")).click();
    
    driver.findElement(By.xpath(".//*[@id='quoteImgFrmId']/div/span")).click();
    
    Robot robot = new Robot();
    
    String sFileName = "Apache.PNG";
    
    type(robot, sFileName);
    
    robot.keyPress(KeyEvent.VK_ENTER);
    
    robot.keyRelease(KeyEvent.VK_ENTER);
    
    }
    public static void type(Robot robot, String FileName){
    
    // Write you own method here to press the keys of the file name on some location on yr computer
    
    robot.keyPress(KeyEvent.VK_F);
    
    robot.keyRelease(KeyEvent.VK_F);
    
    robot.keyPress(KeyEvent.VK_I);
    
    robot.keyRelease(KeyEvent.VK_I);
    
    robot.keyPress(KeyEvent.VK_L);
    
    robot.keyRelease(KeyEvent.VK_L);
    
    robot.keyPress(KeyEvent.VK_E);
    
    robot.keyRelease(KeyEvent.VK_E);
    
    }
    
    }
    

    You need to write your own method to type the file name, you can use robot class, keyboard class, Clipboard class and many more. Google it.

    Regards,
    Lakshay Sharma

    Professor Answered on November 5, 2014.
    Add Comment

    https://www.designerexchange.com/index.php/sell/quote

    Train Answered on November 5, 2014.

    Please use comment section to give extra information.

    on November 5, 2014.
    Add Comment

    Thank you Lakshay..
    Thanks a lottt…  Very helpful… 🙂

    I don’t have 100 points to add a comment ,that’s why I’ve used Answer Section..

    With Regards
    —————–
    A.Ravinder Reddy

    Train Answered on November 5, 2014.

    No Worries, I have changed the settings now. You would be able to do it now.

    Thanks,
    Lakshay Sharma

    on November 5, 2014.

    Thanks a lot …. 🙂 🙂 🙂

    on November 5, 2014.

    I don’t know how to set profile picture in registration.can you please help me this

    on March 7, 2017.
    Add Comment

    I don’t know how to set profile picture in registration.can you please help me this

    Default Answered on March 7, 2017.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.