3 回答

TA贡献1811条经验 获得超6个赞
您可以尝试以下代码:
// wait for the window to appear
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.alertIsPresent());
// switch to the file upload window
Alert alert = driver.switchTo().alert();
// enter the filename
alert.sendKeys(fileName);
// hit enter
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
// switch back
driver.switchTo().activeElement();

TA贡献1810条经验 获得超4个赞
试试这个,
webElement.sendKeys(System.getProperty("user.dir") + "file path");
这里,
webElement 是为文件上传标识的元素。请确保输入元素可见。
尝试将文件路径指定为我们要上传的内容的相对路径。
确保您没有单击浏览按钮,单击浏览按钮将打开 Windows 对话框,其中 selenium webDriver 将不起作用。

TA贡献1828条经验 获得超3个赞
直接使用硒:
driver().findElement(By.id("ContentPlaceHolder1_Uploader1__Insert")).sendKeys(pathToFile);
有了这个,您可以避免使用系统依赖对话框。
添加回答
举报