键盘

时间:2018-02-15 13:24:35

标签: java selenium selenium-webdriver

我无法在此处上传图片。我收到如下所述的错误。你们中的任何人都可以帮助我,因为它非常重要。 代码是,错误也被提到

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


}
}

错误: -

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

3 个答案:

答案 0 :(得分:0)

文件可以使用selenium sendkeys直接上传,如果它接受输入路径,否则你必须去java Robot类上传文件,如下所示。

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

    }

希望这能解决您的问题。

答案 1 :(得分:0)

如果您的元素<p> ... </p>更好地位于<input></input>使用<input>路径中。和绝对的相对路径更好。例如。 &#34; //输入[包含(@class,&#39; some_class&#39;)]&#34; 它适用于我的情况。我使用&#34; // * [包含(@class,&#39; some_class&#39;)]&#34;并改为&#34; //输入[contains(@class,&#39; some_class&#39;)]&#34;它起作用了。

答案 2 :(得分:-2)

似乎您使用的绝对有时指的是输入字段,有时指的是按钮。像你一样的xpath注释不是最佳实践。尝试使用相对xpath。

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

尝试用相对xpath替换它,你的问题应该解决了。如果您粘贴输入字段的html代码,我可以帮助您。