无法从 selenium 中的 pc 上传 excel 文件

Unable to upload a excel file from pc in selenium

为我试过的页面选择 Java 代码

 WebElement element=driver.findElement(By.id("ddlEmailSource"));
        org.openqa.selenium.support.ui.Select se=new org.openqa.selenium.support.ui.Select(element);
        se.selectByValue("ff");
        driver.findElement(By.id("div_btnFileUpload")).sendKeys("C:\Users\Phantom\Documents.txt");

错误详情Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )

HTML 该页面的代码

<input title="Click to Select and Upload File" style="position: absolute; margin: 0px; padding: 0px; opacity: 0; top: 20px; left: 21px;" name="MyFile" type="file">

也试过

driver.findElement(By.name("MyFile")).sendKeys("C:\Users\Phantom\Documents.txt");

但它也通过错误详细信息Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )

尝试过

WebElement elementfile= driver.findElement(By.name("datafile"));
    elementfile.sendKeys("C:\Users\Phantom\Documents.txt");

附上给定的错误详细信息

请给我建议怎么做

所以您只需使用 \,您的代码就可以工作了

WebElement elementfile= driver.findElement(By.name("datafile"));
    elementfile.sendKeys("C:\Users\Phantom\Documents\1.txt");

如果您使用 windows,请在路径中使用双反斜杠 (\),如果您使用 linux 或 mac (//)

更好的方法是将文件及其文件夹放在项目本身中,您也可以使用如下路径:-

./src\Documents\1.txt

String testDataFile  = System.getProperty("user.dir"+"\1.txt");
File src = new File(testDataFile)

user.dir 会给你项目当前目录的位置

这意味着您的 excel 文件应该存在于项目文件夹本身中。在项目目录中创建一个文件夹并将此文件粘贴到该文件夹​​中。这样 git 也会将该数据推送到您的存储库,并将由服务器定位。

希望对您有所帮助:)

您好 Qa 测试请更新您的最后一行代码

driver.findElement(By.id("div_btnFileUpload")).sendKeys("C:\Users\Phantom\Documents.txt");

driver.findElement(By.id("div_btnFileUpload")).sendKeys("C:\Users\Phantom\Documents\1.txt");

这将解决您的问题 facing.Also 请注意,在 Java 中使用 selenium 始终使用双斜杠“\\”。

更新:

Selenium 中的文件上传可以通过两种方式完成:

1. via sikuli or autoit tool (Basically windows automation tool).
2. direct upload when the tag has one attribute **type=file**

在你的例子中,你试图通过按钮而不是按钮进行文件上传 带有属性 type=file 的标签,这就是为什么每次都会打开 window 弹出窗口的原因 当你 运行 上面的代码。而不是进行文件上传只需像下面那样(如您的屏幕截图所示)

driver.findElement(By.name("MyFile")).sendKeys("C:\Users\Phantom\Documents\1.txt");

或者,如果这不起作用,请使用

driver.findElement(By.xpath("//*[@id='div_btnFileUpload']/input[2]")).sendKeys("C:\Users\Phantom\Documents\1.txt");

希望现在对您有所帮助。

您可以使用第三方应用程序 AutoIt 来完成此任务。

您的 AutoIt 脚本应如下所示

WinWaitActive("Choose File to Upload"); //File Upload is the dialog's title
Send("C:\Users\xxx.xxx\Documents\filename.csv");
Send("{ENTER}");

@QATesting - 您可以按如下方式更新您的代码:

它应该对我有用。并根据您的要求优先考虑您的测试方法。举个例子,我在这里将优先级设置为@Test(priority = 1)。我希望它对你有用。

    @Test(priority = 1)
    public void CERTIFICATIONSSCREENUploadCertficationFilesValidation()
            throws InterruptedException, AWTException {

        //Click on File Upload Button
        driver.findElement(By.xpath("//*[@id='certificationFile']")).click();
        Thread.sleep(1000);
        // Set the file name in the clipboard. Also following line of code will search file in your computer so make sure you provide correct file path.

        StringSelection s = new StringSelection("C:\Doc\CertificationFile.xls");
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(s, null);
        Thread.sleep(1000);

        Robot robot1 = new Robot();
        robot1.keyPress(KeyEvent.VK_ENTER);
        robot1.keyRelease(KeyEvent.VK_ENTER);
        robot1.keyPress(KeyEvent.VK_CONTROL);
        robot1.keyPress(KeyEvent.VK_V);
        robot1.keyRelease(KeyEvent.VK_V);
        robot1.keyRelease(KeyEvent.VK_CONTROL);
        robot1.keyPress(KeyEvent.VK_ENTER);
        robot1.keyRelease(KeyEvent.VK_ENTER);
        Thread.sleep(1000);
}