2

我正在開發一個應用程序,該應用程序將用於使用Selenium在Java中自動執行表單填充操作。目前,我已經將其設置爲在拇指驅動器上便攜。我的代碼如下:對Main方法中的方法調用不可見的文件路徑聲明

package AutoFill; 

import java.io.File; 
import java.util.concurrent.*; 
import javafx.application.Application.*; 
import javafx.application.*; 
import javafx.stage.Stage; 
import org.openqa.selenium.*; 
import org.openqa.selenium.remote.CapabilityType; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.openqa.selenium.ie.*; 
import org.openqa.selenium.ie.InternetExplorerDriver.*; 

public class Login extends Application { 

    public static final File file = new File("E:/IEDriverServer_Win32_2.53.1/IEDriverServer.exe");  //path to IEDriver on USB stick 
    public static final DesiredCapabilities desCaps = DesiredCapabilities.internetExplorer();   //new desired capabilities object to set IEDriver run params 
    public static final WebDriver driver = new InternetExplorerDriver(desCaps);       //new IEDriver instance 
    public static final String url = new String("url_here");         //starting url 


@Override 
public void start(Stage primaryStage) { 
    primaryStage.show(); 

} 

public void setup() { 

    File file = new File("E:/IEDriverServer_Win32_2.53.1/IEDriverServer.exe"); 
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());    //force IEDriver path 
    setIEDesCaps(desCaps);               //run cap setter method 
    driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL,"0")); //set screen zoom to 100% to resolve webdriver errors 
    driver.get(url);                 //navigate to url 



} 


public void setIEDesCaps(DesiredCapabilities desCaps) {        //setter method to establish IE webdriver run params     

    desCaps.setPlatform(org.openqa.selenium.Platform.WINDOWS); 
    desCaps.setCapability("EnableNativeEvents", false); 
    desCaps.setCapability("ignoreZoomSetting", true); 
    desCaps.setJavascriptEnabled(true); 

} 

} 

當運行(當然與真正網址,)這個代碼,Eclipse中生成以下錯誤:

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.ie.driver system property; 

其實我已經嘗試手動設置驅動器路徑在兩個不同的實例中 - 最開始時是一個靜態變量(這似乎最明智),以及在主setup()方法中。單獨(當註釋掉其中一個或另一個時),主要方法都不能看到驅動程序路徑的位置。在我將Login類變爲Application的擴展之前,此驅動程序路徑是可見的並且在此代碼的先前版本中工作。

如何在當前代碼中定位文件路徑,使其對主方法可見?我覺得我在這裏錯過了一些東西。

+0

'file.getAbsolutePath()'返回什麼?當路徑已經是絕對路徑時,使用它似乎是多餘的。 – JeffC

+0

你爲什麼要申報兩次?曾經作爲課堂上的「最後一課」,並曾在「setup()」中使用過? – JeffC

+0

在編譯之前,'file'被聲明瞭兩次,但在任何一個實例中被註釋掉了。雙聲明用於測試是否將其置於'setup()'中。 如果我沒有弄錯,'file.getAbsolutePath()'正被用於將'file'路徑傳遞到'System.setProperty()'。儘管如此,這是可回收的代碼,而且您可能對冗餘很有幫助。也許我應該切掉中間人,直接用絕對路徑替換'System.setProperty()'?這應該允許我一起跳過聲明。 – drs

回答

1

在類中聲明變量爲Public Static Final,您應該可以在main方法(classname.variablename)中訪問它。 如果您不使用Property文件,那將是理想的地方。

如果你能分享Main()方法,我可以進一步研究。

注:目前我無權發表評論,因此不得不發帖。儘管這可能不是一個完整的答案。

+0

這實際上比評論格式更適合答案格式。 – dorukayhan

+0

在這種情況下,主要的方法是'setup()',或者我認爲。是的,我假設在類中聲明爲Public Static Final,它將在'setup()'中可見。情況並非如此嗎?我應該聲明一個特定的'Main(String [] args)'而不是? – drs

+0

好的,這裏是我想問題的可能。您甚至在設置webdriver.ie.driver的路徑之前嘗試啓動瀏覽器。public static final WebDriver driver = newInternetExplorerDriver(desCaps);在啓動驅動程序之前,您可能需要設置屬性webdriver.ie.driver。 – Amit