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

eoigrqb6  于 2021-07-13  发布在  Java
关注(0)|答案(5)|浏览(305)


为我尝试的页面选择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\1.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\1.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\1.txt");

所附错误详细信息
请给我建议怎么办

fxnxkyjh

fxnxkyjh1#

所以你就用 \\ 你的代码也能用

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

qncylg1j2#

使用双反斜杠( \\ )如果您使用的是windows,则在path中;如果您使用的是linux或mac,则使用正斜杠( // )
更好的方法是把你的文件和它的文件夹放在你的项目中,你也可以使用路径作为below:-

./src\\Documents\\1.txt

String testDataFile  = System.getProperty("user.dir"+"\\1.txt");
File src = new File(testDataFile)
``` `user.dir` 将给你项目的当前目录的位置
这意味着您的excel文件应该存在于项目文件夹中。在项目目录中创建一个文件夹,并将此文件粘贴到该文件夹中。这样,git也会将数据推送到您的存储库,并通过服务器进行定位。
希望对您有所帮助:)
esyap4oy

esyap4oy3#


嗨qa测试plz更新你的最后一行代码

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

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

这将解决您面临的问题。同时请注意,在使用selenium的java中,始终使用双斜杠“\”。
更新:
selenium中的文件上载可以通过两种方式完成:

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

在您的例子中,您尝试在按钮上而不是属性type=file的标记上进行文件上载,这就是为什么每次运行上述代码时都会弹出一个窗口。要上传文件,只需执行以下操作(如屏幕截图所示)

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

希望这对你有帮助。

o2g1uqev

o2g1uqev4#

您可以使用第三方应用程序autoit来完成此任务。
您的autoit脚本应该如下所示

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

gudnpqoy5#

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

相关问题