【问题标题】:How to get the name of the WebElement variable name in another class如何在另一个类中获取 WebElement 变量名的名称
【发布时间】:2018-06-27 07:58:25
【问题描述】:

我正在尝试将 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

【问题讨论】:

    标签: java selenium selenium-webdriver webdriver


    【解决方案1】:

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

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 2019-07-22
      • 1970-01-01
      相关资源
      最近更新 更多