亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Selenium 上傳文件:找不到文件

Selenium 上傳文件:找不到文件

慕工程0101907 2022-06-30 11:24:44
我有以下使用硒上傳圖像的方法。public static void uploadSampleImage(StaticSeleniumDriver driver){    File file = new File(System.getProperty("user.dir") + "/resources/images/" + SAMPLE_DOCUMENT_FILE_NAME);    Utils.Log("file exists: " + file.exists());    String imagePath = file.getAbsolutePath();    WebElement input = driver.findElement(By.name("file"));    input.sendKeys(imagePath);}這是提供文件路徑的標準方式(如 Guru99 教程中所述)以上傳文件。在 Windows 上進行本地測試時效果很好在 docker 容器(linux)中運行時它不起作用,出現此錯誤:org.openqa.selenium.InvalidArgumentException:無效參數:找不到文件:/usr/src/app/resources/images/image2.png(會話信息:chrome=72.0.3626.81)(驅動程序信息:chromedriver=2.46.628388(4a34a70827ac54148e092aafb70504c4ea7ae926 ),platform=Linux 4.9.125-linuxkit x86_64) (警告:服務器沒有提供任何堆棧跟蹤信息)這很奇怪,因為我確定給定目錄中存在文件(在我上面的方法中,我正在檢查文件是否存在并且日志清楚地確認了這一點)
查看完整描述

2 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

因為RemoteWebDriver你必須設置文件檢測器driver.setFileDetector(new LocalFileDetector());。你的代碼:


public static void uploadSampleImage(StaticSeleniumDriver driver)

{

    driver.setFileDetector(new LocalFileDetector());

    File file = new File(System.getProperty("user.dir") + "/resources/images/" + SAMPLE_DOCUMENT_FILE_NAME);

    Utils.Log("file exists: " + file.exists());


    String imagePath = file.getAbsolutePath();

    WebElement input = driver.findElement(By.name("file"));

    input.sendKeys(imagePath);

}


查看完整回答
反對 回復 2022-06-30
?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

而不是在路徑字符串中使用“/”,您可以使用File.separatorwhich 在引擎蓋下自動處理操作系統級別的文件分隔符。使用它,您的代碼變得獨立于任何操作系統,它讓 Java 負責根據操作系統使用什么分隔符,而不用擔心它。

所以第一行代碼變成了:

new File(System.getProperty("user.dir") + File.separator + "resources" + File.separator + "images" + File.separator + SAMPLE_DOCUMENT_FILE_NAME);

其余部分保持不變。

!!沒有額外的頭痛。??


查看完整回答
反對 回復 2022-06-30
  • 2 回答
  • 0 關注
  • 314 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號