无论我使用哪种定位器,我都无法使用Selenium单击此按钮

时间:2018-10-03 18:28:37

标签: selenium xpath testng href findby

无论我使用什么定位器,我都无法单击此按钮,并且我无法理解原因。我使用过linkText,partialLinkText,x路径,className等。我在Page Object Model设计模式中使用PageFactory。

这是按钮所在的div的代码:

<div class="clearfix main-page-indent">
<a href="http://automationpractice.com/index.php?controller=address" title="Add an address" class="btn btn-default button button-medium">.   <span>Add a new address<i class="icon-chevron-right right"></i></span></a>
</div>

这里是源代码的链接: 查看来源:http://automationpractice.com/index.php?controller=addresses

您只需要首先创建一个帐户,它的超级快。

这是我尝试过的两种方法:

1。)

//Page Factory
@FindBy(how = How.PARTIAL_LINK_TEXT, using = "Add an address")
WebElement newAddressBtn;

public AddNewAddressPage clickAddNewAddressBtn() {
    newAddressBtn.click();
    return new AddNewAddressPage();
}

2。)

@FindBy(xpath = "//a[@href='http://automationpractice.com/index.php?controller=address']")
WebElement newAddressBtn;

有人知道如何整合此定位器吗?

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

能否请您在xpath下尝试找到您的元素?

    @FindBy(xpath = ".//*[@id='center_column']/div[2]/a")
    public WebElement addressNew;

答案 1 :(得分:-1)

感谢您的建议,但我发现了我的错误。并不是无法解决该定位器,而是因为没有初始化PageFactory变量,所以出现了空指针异常。我添加了此构造函数来解决此问题。这样,在实例化页面类时,将初始化所有WebElement。

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