将 Actions 变量发送到方法

Sending Actions variable to a method

我只想知道如何将 Actions 变量发送到另一个方法。示例代码如下

public static void main(String[] args) {
    Actions a = new Actions(driver); \.....line 1
    methodA();
}
static void methodA(){
    a.moveEelement(driver.findElement(By.xpath(" some path ").click().build().perform(); //error line
}

如上所述,带有注释 \error line 的行是问题所在,因为变量 a 不是 main 方法的一部分。

第 1 行声明变量 a 时关键字 static 错误。 Selenium 提到只能使用 Final,但我的情况不行。

我需要关于如何允许我在方法 methodA

中使用变量 a 的建议

在 main 方法之外实例化 Action 变量 a

public static void main(String[] args) {
    Actions a = new Actions(driver); \.....line 1
    methodA();
}

应该如下所示:


static Actions a = null;
public static void main(String[] args) {
    a = new Actions(driver);
    methodA();
}

无法在 "methodA" 中访问变量 "a"。请按以下方式传递。

public static void main(String[] args) {
    Actions a = new Actions(driver); \.....line 1
    methodA(a);
}
static void methodA(Actions a){
    a.moveEelement(driver.findElement(By.xpath(" some path ").click().build().perform(); //error line
}