如何在Java PageFactory中使用By而不是WebElement?

时间:2014-03-25 18:52:15

标签: webdriver

这有效:

@CacheLookup
@FindBy(id = "title") 
private WebElement title; 

但我正在尝试这样的事情:(它不起作用)

@CacheLookup
@FindBy(By.id("title")) 
private By titleBy;  

非常感谢任何帮助。 谢谢

2 个答案:

答案 0 :(得分:0)

我认为你正在寻找类似的东西:

@FindBy(how = How.NAME, using = "q")
private WebElement searchBox;

右?它位于https://code.google.com/p/selenium/wiki/PageFactory

答案 1 :(得分:0)

首先,(来自你的评论)

不建议使用

 webDriver.findElement(By.id("xxxx")), or webDriver.findElement(element)

当您使用Page factory时。只需直接执行操作,并通过 PageFactory.initElements()方法处理webelements布线。

check this了解详情。

来到你的问题,

根据this official doc

@FindBy(By.id("title")) 

不存在。如果您需要替代方法,请尝试olyv。

建议的如何