用於在 Selenium 中註冊 gecko 驅動程式的命令是什麼?


我們可以在 Selenium webdriver 中註冊一個 gecko 驅動程式。對於版本高於 47 的 Firefox,我們可以使用 geckodriver.exe 檔案在 Firefox 中執行測試。要下載這個可執行檔案,請訪問以下連結 - https://github.com/mozilla/geckodriver/releases

接下來,我們必須選擇與我們的本地作業系統相容的 zip 檔案的連結。下載完 zip 檔案後,必須將其解壓縮,並將檔案 - geckodriver.exe 儲存到一個位置。

要註冊這個 geckodriver.exe 檔案,我們必須使用 System.setProperty 方法設定 geckodriver.exe 檔案的路徑。我們還必須建立 FirefoxDriver 類的例項。

WebDriver driver = new FirefoxDriver();

語法

System.setProperty("webdriver.gecko.driver", "<path of geckodriver.exe>");

示例

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class RegGecko{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
         "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://tutorialspoint.tw/index.htm");
      String t = driver.getCurrentUrl();
      System.out.println("Current URL is: " + t);
      //close browser
      driver.close();
   }
}

輸出

更新日期:08-Apr-2021

298 次瀏覽

開啟您的職業生涯

完成課程並獲得認證

開始
廣告