首页 > 解决方案 > Selenium:无法从 Intellij 中 Java 项目的本地目录上传文件

问题描述

System.out.println("LC13.2 started");
                //Locate the element name 'Dokument Auswahlen'
                WebElement DokUpload = driver.findElement(By.xpath(property.getProperty("DokumentSelection_ButtonXpath")));
                String FilePath = ".\\resources\\SyncPilot_Showformular_Testing.pdf";
                DokUpload.sendKeys(FilePath);

上面的代码试图从 Intellij 的本地项目目录中找到一个元素并上传一个文件,但它失败了。任何建议怎么做?在此处输入图像描述

标签: javaselenium

解决方案


使用 sendKeys 上传文件需要绝对路径,我们发送的是位置而不是文件。所以当它到达前端当前目录时不会是你的硒当前目录..所以那个路径中不会有任何文件

使用路径生成器创建绝对路径

System.getProperty("user.home") + /file/relativepath/file.pdf


推荐阅读