我正在尝试将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
答案 0 :(得分: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);
}
}
答案 1 :(得分:0)
尝试使用Action
类单击元素。
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;
}
}