java - 如何获取另一个类中的WebElement变量名的名称

标签 java selenium selenium-webdriver webdriver

我正在尝试将 webElement 名称传递给另一个类以进行 Webdriver 操作。我正在使用 pagefactory 模型。

我想在另一个类中也打印 webelement 变量的名称。

下面是我的代码。

A 类:

Class A{

     @FindBy(how = How.XPATH, using = "//div[text()='Example_23']")
     public WebElement exampleTab;
}

B 类:

class B{

      public static void Click(WebElement objName) throws Exception
      {
            objName.click();
            System.out.println("Clicked on"+ objName);
      }
}

期望的输出:

Clicked on exampleTab

实际输出:

Clicked on com.sun.proxy.$Proxy14

最佳答案

你可以使用下面的代码来做到这一点:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

 class A {

    public WebDriver driver;

    @FindBy(how = How.XPATH, using = "//div[text()='Example_23']")
    public WebElement exampleTab;

    public void initElements(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);

    }

}

public class B {


    public static void main(String r[]) {
        A a = new A();

        System.setProperty("webdriver.chrome.driver",
                   "D:\\ECLIPSE-WORKSPACE\\playground\\src\\main\\resources\\chromedriver-2.35.exe");
        WebDriver driver = new ChromeDriver();
        a.initElements(driver);  // instantiating class A elements

        driver.navigate().to("url");
        driver.manage().window().maximize();

        Click(a.exampleTab);

    }

    public static void Click(WebElement  objName) throws Exception {
        objName.click();

        System.out.println("Clicked on" + objName);
    }
}

关于java - 如何获取另一个类中的WebElement变量名的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51057500/

相关文章:

python - 在 Selenium 中显示当前光标位置

python - 如何使用 python selenium 缩小页面

selenium - 为什么没有 ExpectedConditions 方法返回元素可见性的 bool 值?

Java Http 重定向

java - SimpleMappingExceptionResolver 无法解析 404

java - Selenium 服务器在被 SIGTERM 杀死后不绑定(bind)到套接字

javascript - 计算 ng-repeat 使用 Selenium webdriver 生成的选项卡集中的选项卡数量

java - 强制 Java/Eclipse 在堆栈跟踪中提供更多信息

java - 网格布局中的间距问题

django - 如何在 django 项目中使用 Selenium(LiveServerTestCase) 并行运行测试?