Sh87
Sh87

Reputation: 326

How to verify that urls are redirected its respective page in Selenium?

I am trying to automate - verification of top nav links of https://www.zillow.com. enter image description here

For that I am matching actual urls with expected urls. But in page class method this line retruns null. I don't get it why?

String urlp = locator.all_topnav_links.get(i).getAttribute("href");

Method in page class:

public void verify_topnav_links() {
        System.out.println("Started");
        for (int i = 0; i < locator.all_topnav_links.size(); i++) {
            System.out.println(locator.all_topnav_links.size());
            if (locator.all_topnav_links.get(i).isDisplayed() == true) {
                String link=locator.all_topnav_links.get(i).getText();
                System.out.println(link);
                String urlp = locator.all_topnav_links.get(i).getAttribute("href");
                System.out.println("Links are : " + urlp);
                // now click link one by one
                locator.all_topnav_links.get(i).click();
                driver.navigate().back();
                // 2 verify before url with current opened url
                //Assert.assertEquals(urlp, driver.getCurrentUrl());

            }

Locator in locator class:

@FindAll({@FindBy(xpath = "//ul[@data-zg-section='main']/li")})
    public List<WebElement> all_topnav_links;

Test case in test class:

@Test
    public void verify_TopNav_links()
    {

        hpage.verify_topnav_links();
    }

Also I am not sure whether this correct approach or not. Can anybody tell me the best approach to verify this scenario.

Note: This zillow.com will starts to display captcha if we open multiple times using selenium. To ignore it manually remove catpcha parameters from the url when test case is getting executed.

Upvotes: 1

Views: 1261

Answers (1)

scilence
scilence

Reputation: 1115

It appears you need to use

//ul[@data-zg-section='main']/li/a

here

@FindAll({@FindBy(xpath = "//ul[@data-zg-section='main']/li")})
    public List<WebElement> all_topnav_links;

This xpath

//ul[@data-zg-section='main']/li

Seems to contain multiple elements. But you're trying to access the href attribute directly. The li here doesn't have a href attribute.

Screenshot

Upvotes: 2

Related Questions