为什么在为 gecko 驱动程序声明系统属性时出现语法错误

Why I am getting syntax error while declaring System properties for gecko driver

我已经编写了这段代码来制作 Firefox 驱动程序。但是 Eclipse 在以下行抛出错误:System.setProperty.

错误如下: 此行有多个标记 - 标记“.”的语法错误,@ 应在此标记之后 - 语法错误,插入 "SimpleName" 完成 限定名称 - 语法错误,插入"Identifier ("完成 方法头名称 - 语法错误,插入“)”来完成MethodDeclaration - 令牌“,”的语法错误,<预期 我已经正确设置了构建路径和 JAR。我仍然收到此错误。

package testing;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Login {

    WebDriver driver = new FirefoxDriver();
    System.setProperty("webdriver.gecko.driver", "G:\JARs\geckodriver.exe");

}

您的代码似乎没有包含 main 方法。 它应该是这样的:

package testing;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Login {

    public static final main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        System.setProperty("webdriver.gecko.driver", "G:\JARs\geckodriver.exe");
    }

}