当我尝试在selenium with java中使用sendkeys上载新文件时,它在本地工作,但如果我在jenkins server中运行相同的脚本,则得到的异常为“org.openqa.selenium.invalidargumentexception:invalid argument:file not found:”。
下面是我编写的代码片段,用于获取文件路径并使用sendkeys传递文件路径。
private void bulkUpload(String filePath, By locator) throws InterruptedException {
WebElement input = action.findWebElement(locator, "Upload");
JavascriptExecutor jsx = (JavascriptExecutor) action.getWebDriver();
jsx.executeScript("arguments[0].style.display = 'block';", input);
Thread.sleep(1000);
input.sendKeys(filePath);
}
提前谢谢你的帮助
2条答案
按热度按时间bqf10yzr1#
vsreekanth尝试发送绝对文件路径。我一直在用这种方法,它在本地和Jenkins工作。
我希望它对你有用。
路径参数是资源中的文件,例如:
"src/main/resources/files/sampleFile.txt"
```public void uploadFile(WebElement input, String path) {
WebElement btnUpload = input.findElement(By.xpath(".."))
.findElement(By.xpath("..")).findElement(By.tagName("button"));
String script = "arguments[0].style.display = 'block';";
obtemJs().executeScript(script, input);
input.sendKeys(new File(path).getAbsolutePath());
}
mznpcxlj2#
试着用下面的方法来解决这个问题,我也遇到了同样的问题