Java 如何在另一个类中获取WebElement变量名的名称
我正在尝试将webElement名称传递给另一个类以进行Webdriver操作。我正在使用pagefactory模型 我想在另一个类中打印webelement变量的名称 下面是我的代码 A类: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
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;
}
}