我正在开发一个应用程序,该应用程序将用于使用 Selenium 在 Java 中自动执行表单填写操作。我目前已经将两者都设置为在拇指驱动器上便携。我的代码如下:
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);
}
}
运行此代码时(当然,使用真实的 url),Eclipse 会生成以下错误:
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.ie.driver system property;
我实际上尝试在两个不同的实例中手动设置驱动程序路径 - 一开始是作为静态变量(这似乎是最明智的),以及在主 setup() 方法中。单独地(当注释掉一个或另一个时),驱动程序路径的放置对 main 方法都是不可见的。在我将 Login 类转换为 Application.
如何在当前代码中定位文件路径,使其对 main 方法可见?我觉得我在这里遗漏了一些东西。