file upload在jenkins run中不能与java selenium sendkeys一起工作,但在本地工作

kcrjzv8t  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(312)

当我尝试在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);
  }

提前谢谢你的帮助

bqf10yzr

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

mznpcxlj

mznpcxlj2#

试着用下面的方法来解决这个问题,我也遇到了同样的问题

  • (如果输出时出现类强制转换异常,请尝试在上载文件的方法中获取web元素)
WebElement element = driver.findElement(By.id("file-upload"));
LocalFileDetector detector = new LocalFileDetector();
String path =new File("src/test/resources/testdata/sampleDocument.docx").getAbsolutePath();
File file = detector.getLocalFile(path);
((RemoteWebElement) element).setFileDetector(detector);
element.sendKeys(file.getAbsolutePath());

相关问题