0

实际上我想获得一个@FindBy用于页面对象模式的元素。

我有 2 个类,第一个是我命名TestPage的页面对象,第二个是命名的PageSaveTest(我的测试发生的地方并调用TestPage)。

我也尝试过使用@FindBywithxpathid

>> 这是我的测试页

import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class TestPage {

  // get autocomplete input 
  @FindBy(css = "input[id*='supplierOps_input']")
  private WebElement autocompleteSupplierOps;

  // getter
  public WebElement getAutocompleteSupplierOps() {
    return autocompleteSupplierOps;
  }

}

>> 这是我的 PageSaveTest

// How i "inject" my TestPage
@Page
TestPage testpage;

[...]
// My test
WebElement autocomplete = testpage.getAutocompleteSupplierOps();

String keys = "OP";
autocomplete.sendKeys(keys); // >>>>>>> Error throwed here !                
List<WebElement> listSugg = testpage.getSuggestionsSupplierOps();

错误信息 :

org.openqa.selenium.NoSuchElementException : Returned node was not an HTML element.

我的想法 :

我认为麻烦来自于@FindBy. 但是我使用这个例子来构建我的 TestPage 和我的测试以及这个

问题:有人可以向我解释如何@FindBy在我的示例中使用和使用吗?石墨烯的文档真的很差。


编辑 :

我已经在TestPage(上图)中修改了我的 getter,我尝试过简单打印 id 属性值,例如

public WebElement getAutocompleteSupplierOps() {
  System.out.println(">>>> "+autocompleteSupplierOps.getAttribute("id"));
  return autocompleteSupplierOps;
}

但仍然是同样的错误,@FindBy被搞砸了。

在此问题中添加的另一个 @FindBy 规范。


更新 :

我已经修复了我的选择器,但实际上驱动程序会话存在问题,例如:

             page2.getAutocompleteSupplierOps();
   PAGE 1   ---------------------------------->   PAGE 2
driver id:1 ----------------------------------> driver id:2
                                                driver.showPageSource() is empty
return no element found <---------------------- driver.findElement() -> not found    

我使用了 3 种不同的方式,the @FindBy,the@Drone WebDriver最后是@Lukas Fryc对我建议的方式。

4

2 回答 2

2

您可以尝试直接使用驱动程序,而不是注入WebElementusing @FindBy

WebDriver driver = GrapheneContext.getProxy(); // this will be removed in Alpha5 version, use `@Drone WebDriver` instead
WebElement autocompleteSupplierOps = 
    driver.findElement(By.css("input[id*='supplierOps_input']"));

但它应该给您与 do 相同的结果@FindBy- 但是它会检查问题不是由注入引起的,而是出现了其他一些问题。

您可能有错误的 CSS 选择器 - CSS 选择器的支持取决于使用的浏览器及其版本。

您尝试查找的节点不必在页面中,您可能需要等待它才会出现使用等待 API 或请求保护。

最佳实践是在开发中使用远程可重用会话和真实浏览器 - 它可以快速揭示原因。

于 2013-04-30T05:19:31.093 回答
0

我认为@FindBy(css ="...")您可以尝试而不是使用@FindBy(xpath="...")它,我发现它更可靠。

于 2017-08-16T22:24:24.660 回答