Search code examples
seleniumxpathtestnghreffindby

I cannot click on this button using Selenium no matter what Locator I use


I cannot click on this button no matter what locator I use and I'm having trouble understanding why. I've used linkText, partialLinkText, x-path, className, etc. I'm using PageFactory in a Page Object Model design pattern.

Here is the code for the div the button is in:

<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>

Here is a link to the source code: view-source:http://automationpractice.com/index.php?controller=addresses

You just have to create an account first, its super quick.

Here are two of the methods I have tried:

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;

Anyone have any idea how to consolidate this locator?

Thanks for your help!


Solution

  • thank you for your suggestions but I found my error. It was not that the locator could not be resolved, but rather I was getting a null pointer exception because I failed to initialize my PageFactory variables. I added this constructor to fix the problem. That way when the page class is instantiated all of the WebElements will be initialized.

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