Selenium WebDriver - 无法使用 PageObjectModel 访问或单击元素 [英] Selenium WebDriver - Not able to access or click elements using PageObjectModel

查看:166
本文介绍了Selenium WebDriver - 无法使用 PageObjectModel 访问或单击元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have defined Web Elements using Page Object model in Selenium. From test method, when ever I try to access or perform any action on those web elements my test completely skips it and completes, with no error.

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();

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

Is there some set up that I'm missing?

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

Update after correcting catch message-- I get following null exception Cannot invoke "org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)" because "this.searchContext" is null

推荐答案

在您的 Test1 类中,您使用字段初始化来初始化 HomePage 类,所以 @FindBy(xpath="//button[@id='sparkButton']") 找不到网络元素.当你在测试方法中调用 clickHomepagemenuDropDown() 时,它会抛出异常并进入 catch 块.在 catch 块中,您刚刚编写了 return.出于这个原因,测试跳过并完成,没有错误.我认为您应该在测试方法中初始化页面对象.

In your Test1 class you initialized HomePage class using field initializing so @FindBy(xpath="//button[@id='sparkButton']") can not find the web element. When you call clickHomepagemenuDropDown() in the test method, it throws an exception and it enters the catch block. In the catch block you just wrote return. For this reason the test skips and completes, with no error. I think you should initialize your page objects in your test methods.

这篇关于Selenium WebDriver - 无法使用 PageObjectModel 访问或单击元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆