问题描述
我尝试给 popup.jpg 文件提供一个相对路径,但它总是崩溃,找不到文件,互联网也没有给我答案,所以我决定在这里提问
这是我的代码:
public class WrongDate
{
public static void main(String[] args) throws MalformedURLException,InterruptedException,FindFailed
{
//String page = "C:\\Users\\kamil\\OneDrive\\Dokumenty\\eclipse - proj\\Project\\popup.jpg";
String page = "./eclipse - proj/Project/popup.jpg";
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.fandom.com/register?redirect=https%3A%2F%2Fwww.fandom.com%2Fexplore-pl%3Fuselang%3dpl%26cb%3D1618719439872");
driver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]")).click();
driver.findElement(By.id("signupEmail")).sendKeys("");
driver.findElement(By.id("signupUsername")).sendKeys("");
driver.findElement(By.id("signupPassword")).sendKeys("");
driver.findElement(By.name("birthdate")).click();
driver.findElement(By.name("month")).sendKeys("89");
driver.findElement(By.name("day")).sendKeys("48");
driver.findElement(By.name("year")).sendKeys("1567");
driver.findElement(By.id("signupSubmit")).click();
Screen a = new Screen();
a.find(page);
//String b = driver.findElement(By.xpath("//*[@id=\"signupForm\"]/div[3]/small")).getText();
System.out.println(page);
}
};
解决方法
String currentWorkingDir = System.getProperty("user.dir");
将为您提供当前工作目录,在这种情况下,您的项目文件夹将是当前工作目录。
您所要做的就是放入(假设在您的项目文件夹中是 image.jpg) 并像这样定义名称:
String imageName = abc.jpg
并使用这样的相对路径:
a.find(currentWorkingDir + "\\" + imageName);