<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 如何上傳&使用 Selenium Webdriver 下載文件 > 原文: [https://www.guru99.com/upload-download-file-selenium-webdriver.html](https://www.guru99.com/upload-download-file-selenium-webdriver.html) 在本教程中,我們將學習如何處理文件上載和下載。 ## 上載檔案 在本節中,我們將使用 [http://demo.guru99.com/test/upload/](http://demo.guru99.com/test/upload/) 作為我們的測試應用程序。 該站點可以輕松地使任何訪問者上傳文件而無需他們進行注冊。 **在 WebDriver 中上傳文件只需在文件選擇輸入字段上使用 sendKeys()方法輸入要上傳文件的路徑即可。** <figure>![](https://img.kancloud.cn/88/5f/885ff62b8ec8134afaf628110108519b_412x304.png) <figcaption>處理 Selenium Webdriver 中的文件上傳彈出窗口</figcaption> </figure> 處理 Selenium Webdriver 中的文件上傳彈出窗口 假設我們希望上傳文件“ C:\ newhtml.html”。 我們的 WebDriver 代碼應類似于以下所示。 ``` package newproject; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class PG9 { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe"); String baseUrl = "http://demo.guru99.com/test/upload/"; WebDriver driver = new FirefoxDriver(); driver.get(baseUrl); WebElement uploadElement = driver.findElement(By.id("uploadfile_0")); // enter the file path onto the file-selection input field uploadElement.sendKeys("C:\\newhtml.html"); // check the "I accept the terms of service" check box driver.findElement(By.id("terms")).click(); // click the "UploadFile" button driver.findElement(By.name("send")).click(); } } ``` 運行此腳本后,您應該能夠成功上傳文件,并且應該會收到類似的消息。 ![](https://img.kancloud.cn/76/06/7606a1fc82243ad9eba1d621b197e825_355x80.png) 在 WebDriver 中上傳文件時,請記住以下兩點 1. 無需模擬點擊“瀏覽”按鈕。 WebDriver 自動將文件路徑輸入到<輸入 type =“ file” >元素的文件選擇文本框中 2. 在 Java IDE 中設置文件路徑時,請對反斜杠使用正確的轉義符。 ![](https://img.kancloud.cn/b5/a7/b5a7b4591fed1a54964f2014a5836ff1_434x152.png) ### 下載檔案 **WebDriver 無法訪問瀏覽器在單擊下載鏈接或按鈕時顯示的下載對話框**。 但是,我們可以使用名為“ wget”的單獨程序繞過這些對話框。 #### 什么是 Wget? **Wget 是一個小型且易于使用的命令行程序,用于自動執行下載**。 基本上,我們將從 WebDriver 腳本訪問 Wget 來執行下載過程。 #### 設置 Wget **步驟 1:**在 C 驅動器中,創建一個新文件夾,并將其命名為“ Wget”。 從此處下載 wget.exe [并將其放置在您從上述步驟創建的 Wget 文件夾中。](https://eternallybored.org/misc/wget/) ![](https://img.kancloud.cn/3d/42/3d42812a83f1416e2b98ca215e7c3e29_1084x438.png) **步驟 2:**按下 Windows 鍵+“ R”打開運行; 輸入“ cmd &”,然后單擊“確定”。 ![](https://img.kancloud.cn/02/10/0210d75cf73b675a1c06c7a15405c81d_399x206.png) 輸入命令“ cd /”以移至根目錄 ![](https://img.kancloud.cn/2a/de/2ade68766d49c8b72ab1634d327e9ad2_741x265.png) **步驟 3:**鍵入命令以檢查給定的設置是否正常工作 ``` cmd /c C:\\Wget\\wget.exe -P C: --no-check-certificate http://demo.guru99.com/selenium/msgr11us.exe ``` ![](https://img.kancloud.cn/70/16/701697803e28593959bd32ccf7fec439_602x142.png) 寫入 C 驅動器似乎存在問題。 **步驟 4:**在使用 Selenium Webdriver 執行代碼之前,需要在命令行中調試 wget 錯誤。 這些錯誤將在 Eclipse 中持續存在,并且錯誤消息不會提供足夠的信息。 最好首先使用命令行來使 wget 工作。 如果它可以在命令行中運行,那么它肯定會在 Eclipse 中運行。 在我們的示例中,如步驟 3 所示,寫入 C 驅動器存在問題。 讓我們將下載位置更改為 D 驅動器并檢查結果。 ``` cmd /c C:\\Wget\\wget.exe -P D: --no-check-certificate http://demo.guru99.com/selenium/msgr11us.exe ``` ![](https://img.kancloud.cn/ed/a3/eda362b69174726e68bb4cbc87d587e0_1502x380.png) Messenger 已成功下載。 在繼續之前,請不要忘記刪除下載的文件 ### 使用 WebDriver 和 Wget 在以下示例中,我們將使用 WebDriver 和 wget 下載流行的聊天軟件 Yahoo Messenger。 我們的基本 URL 為 [http://demo.guru99.com/test/yahoo.html](http://demo.guru99.com/test/yahoo.html) 。 ![](https://img.kancloud.cn/6c/7f/6c7f2947f4ebee8737b2d1f316be62ae_556x228.png) **步驟 1** 導入“ java.io.IOException”包,因為稍后在步驟 4 中我們將不得不捕獲 IOException。 ![](https://img.kancloud.cn/c1/8c/c18cce3bce41c4c990b72fe518669db0_196x20.png) **步驟 2** 使用 getAttribute()獲得下載鏈接的“ href”值,并將其另存為 String 變量。 在這種情況下,我們將變量命名為“ sourceLocation”。 ![](https://img.kancloud.cn/db/4f/db4f3773e1a203a49f3fa0a0c72ea057_463x63.png) **步驟 3** 使用以下命令設置 wget 的語法。 ![](https://img.kancloud.cn/5f/c9/5fc9fac886fc063c9019d19f0afb612b_438x170.png) **步驟 4** 通過從 WebDriver 代碼中調用 wget 來啟動下載過程。 ![](https://img.kancloud.cn/94/07/94076955032c92eb3a70b5acde904b94_501x346.png) 總結起來,您的 WebDriver 代碼看起來像下面所示的代碼。 ``` package newproject; import java.io.IOException; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class PG8 { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe"); String baseUrl = "http://demo.guru99.com/test/yahoo.html"; WebDriver driver = new FirefoxDriver(); driver.get(baseUrl); WebElement downloadButton = driver.findElement(By .id("messenger-download")); String sourceLocation = downloadButton.getAttribute("href"); String wget_command = "cmd /c C:\\Wget\\wget.exe -P D: --no-check-certificate " + sourceLocation; try { Process exec = Runtime.getRuntime().exec(wget_command); int exitVal = exec.waitFor(); System.out.println("Exit value: " + exitVal); } catch (InterruptedException | IOException ex) { System.out.println(ex.toString()); } driver.close(); } } ``` 執行完此代碼后,檢查您的 D 驅動器,并驗證 Yahoo Messenger 安裝程序已成功下載到那里。 ![](https://img.kancloud.cn/10/a9/10a99b0c624357f3216896363c2fdbf5_476x381.png) **摘要** * 在 WebDriver 中上傳文件只需在文件選擇輸入字段上使用 sendKeys()方法輸入要上傳文件的路徑即可。 * WebDriver 無法自行自動下載文件。 * 使用 WebDriver 下載文件的最簡單方法是使用 Wget。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看