Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在另一个类中获取WebElement变量名的名称_Java_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

Java 如何在另一个类中获取WebElement变量名的名称

Java 如何在另一个类中获取WebElement变量名的名称,java,selenium,selenium-webdriver,webdriver,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 voi

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

尝试使用
Action
class单击元素

Actions action=new Actions(driver);

    public boolean clickAnelemnt(WebElement el) 
    {
        try{
            action.moveToElement(el).click().build().perform();
            return true;
        }catch(Exception e)
        {
            System.out.println(e);
            return false;
        }
    }