Java:从资源加载 image-url 时无法调用“java.net.URL.getPath()

Java: Cannot invoke "java.net.URL.getPath() when loading image-url from resources

提问人:Анастасия Попкова 提问时间:9/14/2023 更新时间:9/14/2023 访问量:138

问:

当我应该从资源上传文件时,我有编写自动测试的任务。当我运行代码时:

 WaitService waitService = new WaitService(driver);
        AddTestRunPage addTestRunPage = new AddTestRunPage(driver);
        WebElement fileUploadElement = waitService.waitForExists(By.id("libraryAttachmentsAddItem"));
        String pathToFile = PositiveTest.class.getClassLoader().getResource("resources/download.jpeg").getPath();
        fileUploadElement.sendKeys(pathToFile.substring(1,pathToFile.length()));
        addTestRunPage.getButtonSubmit().click();
        Assert.assertEquals("download.jpeg",addTestRunPage.getLabelUploaded().getText());

我有错误:java.lang.NullPointerException:无法调用“java.net.URL.getPath()”,因为“java.lang.ClassLoader.getResource(String)”的返回值为空

请帮忙。我该如何解决?

我也尝试了绝对路径,但我有同样的错误

Java NullPointerException

评论

0赞 sweintritt 9/14/2023
你试过吗String pathToFile = PositiveTest.class.getClassLoader().getResource("download.jpeg").getPath();
0赞 g00se 9/14/2023
尝试String pathToFile = PositiveTest.class.getResource("/resources/download.jpeg").getPath();
0赞 Анастасия Попкова 9/14/2023
@sweintritt使用此代码,我遇到错误:org.openqa.selenium.ElementNotInteractableException:元素不可交互
0赞 Анастасия Попкова 9/14/2023
@g00se使用此代码,我遇到错误:java.lang.NullPointerException:无法调用“java.net.URL.getPath()”,因为“java.lang.Class.getResource(String)”的返回值为null
0赞 g00se 9/14/2023
BKC公司在这种情况下,@sweintritt拥有正确的道路

答: 暂无答案