簡體   English   中英

我已經安裝了Gecko驅動程序,但是仍然出現錯誤

[英]I've installed Gecko driver but still getting an error

我已經安裝了Gecko驅動程序,因為我收到此錯誤:“ java.lang.IllegalStateException:驅動程序可執行文件的路徑必須由webdriver.gecko.driver系統屬性設置;”

但是在應用代碼后,我仍然遇到Gecko驅動程序錯誤。

下面是我的完整代碼。 請讓我知道我想念的東西。

public class Pawan {

    public static WebDriver driver;

    public static void main(String[] args){

    System.setProperty("webdriver.firefox.marionette","C:\\Users\\Lalit-pc\\Desktop\\geckodriver-v0.21.0-win64\\geckodriver.exe");

    driver = new FirefoxDriver();

    }

     @Test
     public void test()  {
         WebDriver driver = new FirefoxDriver();    
      driver.manage().window().maximize();
      driver.get("https://www.google.com/");
      driver.findElement(By.linkText("Find a Physician")).click();
      driver.findElement(By.id("searchZip")).sendKeys("32806");   
      driver.findElement(By.linkText("Mile Radius")).click();
      try{
           Thread.sleep(6000);
          }catch (InterruptedException ie1) {
            ie1.printStackTrace();
          }

      driver.findElement(By.linkText("20")).click(); 
      driver.findElement(By.linkText("Specialty")).click();
      try{
           Thread.sleep(6000);
          }catch (InterruptedException ie1) {
            ie1.printStackTrace();
          }
      driver.findElement(By.linkText("Anesthesiology")).click();
      driver.findElement(By.cssSelector("input[type='submit'][value='Search Now']")).click();
      String str= driver.findElement(By.xpath(".//*[@id='finderListView']/div[3]/div[1]/div/div[1]/p")).getText();

      if("Anesthesiology".equals(str))

          System.out.println("Physician Search Successful");
      else
          System.out.println("Physician Search NOT Successful");

      driver.findElement(By.linkText("Browse Locations")).click();
      try{
           Thread.sleep(6000);
          }catch (InterruptedException ie1) {
            ie1.printStackTrace();
          }
      driver.findElement(By.xpath(".//*[@id='sidebarMenu']/div[1]/form/div/input")).sendKeys("32806");
      driver.findElement(By.xpath(".//*[@id='sidebarMenu']/div[1]/form/input")).click();

        try{
               Thread.sleep(6000);
              }catch (InterruptedException ie1) {
                ie1.printStackTrace();
              } 
      driver.findElement(By.xpath(".//*[@id='sidebarMenu']/div[2]/section/div/ul/li[1]/a")).click();
      try{
           Thread.sleep(6000);
          }catch (InterruptedException ie1) {
            ie1.printStackTrace();
          }   
      WebElement divElement = driver.findElement(By.xpath(".//*[@id='overflow-autoScroll']/li[1]/ul/li/a/div[2]/span[3]"));
      String stri = divElement.getText();
      if(stri.contains("32806"))

          System.out.println("Location Search successful");
      else
          System.out.println("Location Search not successful");

      driver.findElement(By.xpath("html/body/header/div[1]/div[2]/a[3]")).click();   

      driver.findElement(By.linkText("Health Topics")).click();
      try{
           Thread.sleep(6000);
          }catch (InterruptedException ie1) {
            ie1.printStackTrace();
          }

      driver.findElement(By.linkText("Diabetes")).click();
      WebElement divElementtwo = driver.findElement(By.xpath("html/body/div[4]/div/div[1]/div[1]/h2"));
      String strn = divElementtwo.getText();
              if(strn.contains("Diabetes"))        
          System.out.println("Blog Search successful");
      else
          System.out.println("Blog Search not successful");



      }
}

要驗證gecko驅動程序是否與您當前的firefox&selenium版本兼容,請執行以下操作:

如果您使用的是Windows OS,則將下載的gecko驅動程序保留在System32上;如果使用的是Mac OSX,則將文件保留在/ usr / local / bin中

  1. 通過以下命令運行Selenium Standalone服務器: java -jar selenium-server-standalone-3.13.0.jar
  2. 在Firefox瀏覽器中打開以下網址: http:// localhost:4444 / wd / hub
  3. 創建會話並選擇Firefox瀏覽器

如果瀏覽器啟動,則geckodriver與Firefox和Selenium版本的兼容性沒有問題。

JUnit使用的主入口點與您在此處定義的public static void main(String[] args)方法不同,因此,如果執行測試,將不會執行System#setProperty

要為該類中的所有測試一次添加系統屬性,您需要定義一個@BeforeClass方法:

public class Pawan {

    public static WebDriver driver;

    @BeforeClass
    static void init() {
        System.setProperty("webdriver.firefox.marionette","C:\\Users\\Lalit-pc\\Desktop\\geckodriver-v0.21.0-win64\\geckodriver.exe");
    }

    //test cases here...
}

現在,出於開發目的,我建議將此變量設置為PATH環境變量(取決於OS)可訪問的常量,而不是將其設置為系統屬性。

如果不想使用@BeforeClass批注,也可以在@Test中定義geckodriver:

     @Test
     public void test()  {
         System.setProperty("webdriver.firefox.marionette","C:\\Users\\Lalit-pc\\Desktop\\geckodriver-v0.21.0-win64\\geckodriver.exe");
         WebDriver driver = new FirefoxDriver();    
         driver.manage().window().maximize();
         driver.get("https://www.google.com/");

這也將起作用:)

要求進行小的設置更改:(最新的Firefox瀏覽器)

public class Pawan {

public static WebDriver driver;

@BeforeClass
public static setup() {

System.setProperty("webdriver.gecko.driver","C:\\Users\\Lalit-pc\\Desktop\\geckodriver-v0.21.0-win64\\geckodriver.exe");

driver = new FirefoxDriver();

}

// Urs test here

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM