Selenium WebDriver - 无法使用 PageObjectModel 访问或单击元素

问题描述

我在 Selenium 中使用页面对象模型定义了 Web 元素。从测试方法来看,当我尝试访问或对这些网络元素执行任何操作时,我的测试会完全跳过它并完成,没有错误。

public class HomePage extends Base{

@FindBy(xpath="//button[@id='sparkButton']")
    public WebElement menuDropDown;

public HomePage(){
        PageFactory.initElements(driver,this);
    }

public void clickHomepagemenuDropDown() {
        menuDropDown.click();
        System.out.println("Print HELLO");
   }
}


public class Test1 extends Base{

  HomePage homepage = new HomePage() ;
  @Test(priority=1)
  public void homePage() throws Exception {
    try {
      //do something
        homepage.clickHomepagemenuDropDown();
      //print something
       }catch (Exception e) {
               System.out.println (e);
                  return;
       }

}

如果我替换 homepage.clickHomepagemenuDropDown(); 使用以下几行,我的程序将运行良好。

WebElement mdd = driver.findElement(By.xpath("//button[@id='sparkButton']"));
mdd.click();

是否有一些我遗漏的设置?

更正捕获消息后更新-- 我得到以下空异常 无法调用“org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)”,因为“this.searchContext”为空

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)