是否可以通过具有WebElement的对象获取

时间:2013-12-17 16:31:15

标签: java selenium

我正在使用@FindBy注释来查找WebElements。但我想在方法中使用这些WebElements作为参数。只有在页面上找到WebElement时,我才成功。但是,如果我想等待它,我必须使用By(使用相同的定位器)。

E.g。

private static final String SEARCHFIELD_LOC = "#search input[placeholder]";
@FindBy(css = SEARCHFIELD_LOC)
public WebElement searchField;
public By searchField_by = new By.ByCssSelector(SEARCHFIELD_LOC);

这样我可以在某些方法中将此WebElement用作By对象,例如

public boolean isElementDisplayedWithWait(final By by) { .... }

因此,对于每个元素,我有4行代码。我很懒,正在寻找一种简化方法。

2 个答案:

答案 0 :(得分:1)

我找到了另一种解决方案。我将我的定位器存储为字符串。

public String searchField = ".slide.slide_search input[placeholder]"

每个定位器只有一行代码。我希望只支持CSS定位器就足够了。我不会使用@FindBy获取WebElements。相反,我将使用等待元素的特殊方法获取WebElements并记录正在发生的事情。我将实现像这样获取WebElement:

WebElement we = wait.until(ExpectedConditions.visibilityOfElementLocated(new By.ByCssSelector(locator)));

等待是

Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
                .withTimeout(timeout, TimeUnit.SECONDS)
                .pollingEvery(1, TimeUnit.SECONDS)
                .ignoring( StaleElementReferenceException.class ) ;

答案 1 :(得分:0)

QAF formerly ISFW中的扩展webelement可以使用带有webelement本身的wait / assertions / verification方法。例如:

@FindBy(css = SEARCHFIELD_LOC)
public WebElement searchField;

searchField.waitForPresent();
searchField.assertPresent(); //auto wait
searchField.verifyPresent(); //auto wait

参考:

  1. QAF Selenium testing-frameworks
  2. ISFW FAQ
相关问题