2014-03-28 108 views
3

我想用selenium webdriver上传文件。我可以使用文件的绝对路径在本地机器上执行此操作:String filename =“C:\ Windows \ Temp \”+“templatePMT.html”;. 我使用Eclipse和maven项目与pom.xml和。我在SVN上提交这个项目。我使用Jenkins软件在IE8上运行Web应用程序的测试。詹金斯部署在Red Hat 5.0上。 ----问题是:----我如何使用相对路径而不是绝对路径上传文件?该文件位于我的项目的资源文件夹中。Selenium webdriver使用相对路径上传下载文件

回答

0

你可以得到的资源文件的绝对路径:

URL resource = Main.class.getResource("/templatePMT.html"); 
String absolutePath = Paths.get(resource.toURI()).toString(); 
+0

家伙你好,非常感谢你回答... 我想你的建议,但它不工作。这是我在这个时候如何做的,它的工作原理如下: String filename =“C:/test.txt”; File file = new File(System.getenv(“java.io.tmpdir”),filename); ();}点击按钮选择一个文件getWebDriver()。findElement(By.id(“exportNegativeCreditAction_fichier”))。sendKeys(file.getAbsolutePath()); //点击按钮上传文件 getWebDriver()。findElement(By.id(“exportNegativeCreditAction_upload_boutonEnvoyer”))。click(); 你能告诉我如何在远程机器上做到这一点。 – Kazman

1

您可以使用下面的代码来获取文件的绝对路径和上传的内容

String filePath = System.getProperty("user.dir") + "/src/res/test.pdf; driver.findElement(By.id("elementID")).sendKeys(filePath);

你也可以使用使用找到cssSelector以及元素。那么代码将是; driver.findElement(By.cssSelector("input[id='elementId']")).sendKeys(filePath); 查看更多正则表达式在这里:Finding an element by partial id

编号:Relative path of a file to upload a file

+1

谢谢 - 这让我非常接近 - 使用Python我需要使用'os.path.abspath()'。在这里留下这个评论作为其他Python用户的面包屑。 – tvanfosson

相关问题