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