1

I am unable to upload the image here. I am getting the error as mentioned below. Can anyone of you guys help me in it as it is very important. The code is and the error is mentioned too

package kemgo_package;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.omg.SendingContext.RunTime;
//import org.apache.bcel.generic.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class kengo_class 
{
public static void main(String[] args) throws Exception 
{

    System.setProperty("webdriver.gecko.driver","E:\\geckodriver-v0.19.1-win64\\geckodriver.exe");
    System.out.println("Hello");
    WebDriver driver= new FirefoxDriver();
    driver.manage().window().maximize();
    Password
    driver.findElement(By.name("login")).click();
    driver.findElement(By.xpath("html/body/div[1]/div/header/div[2]/nav/div/ul[2]/li[3]/a")).click();
    driver.findElement(By.id("userClearBtn")).click();  //Click Auction
    System.out.println("hello world-----2");

     WebElement UploadImg = driver.findElement(By.xpath("/html/body/div[1]/div/div/div/section/div/form/div[1]/div[2]/div[1]/div[1]/label/p[2]")); //Image upload
     UploadImg.sendKeys("C:\\Users\\Rahul\\Desktop\\Buyer_Detail_View.png");  //Select image
    //driver.findElement(By.xpath("/html/body/div[1]/div/div/div/section/div/form/div[1]/div[2]/div[1]/div[1]/label/p[2]")).sendKeys("C:\\Users\\Rahul\\Desktop\\test Images\\1..jpg");  //Clicks on Browse button
    // Runtime.getRuntime().exec("C:\\Users\\Rahul\\Desktop\\AutoIt\\UploadFile.exe");


}
}

ERROR:-

Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element <p class="btn waves-effect waves-light btn-upload"> is not reachable by keyboard
RAHUL
  • 398
  • 1
  • 6
  • 15

3 Answers3

1

File can be uploaded directly using the selenium sendkeys if its accept to enter the path , otherwise you have to go for java Robot class to upload the file, like below.

public void uploadFile(WebDriver driver, String path) throws AWTException, InterruptedException {
        Robot robot = new Robot();
        // To click on file upload button
        driver.findElement(By.xpath("//div[@class='row_dv text-center ']//p[text()='Picture Upload']")).click();
        // path is the absolute path of the image ex:
        // image(C:\\Users\\Rahul\\Desktop\\Buyer_Detail_View.png)
        Thread.sleep(5000);
        StringSelection stringSelection = new StringSelection(path);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, stringSelection);
        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);

    }

Hope this will solve your problem.

Pradeep hebbar
  • 2,147
  • 1
  • 9
  • 14
0

If your element <p> ... </p> is inside the <input></input> use <input> path better. And better relative path than absolute. E.g. "//input[contains(@class, 'some_class')]" It worked for my case. I used "//*[contains(@class, 'some_class')]" and changed in into "//input[contains(@class, 'some_class')]" and it worked.

Irina
  • 786
  • 1
  • 11
  • 25
-2

It seems like the absolute you are using sometimes refers to an input field and sometimes to a button. An xpath annotation like you have is not best practise. Try using a relative xpath.

/html/body/div[1]/div/div/div/section/div/form/div[1]/div[2]/div[1]/div[1]/label/p[2]

Try replacing this with a relative xpath and your problem should be solved. If you paste the html code of the input field, I can help you out there.

Anand
  • 1,899
  • 1
  • 13
  • 23