如何在Selenium中验证URLs是否被重定向到其各自的页面?

问题描述 投票:0回答:1

我正在尝试自动化--验证顶部导航链接的有效性。https:/www.zillow.com.enter image description here

为此,我将实际的urls和预期的urls进行了匹配。但是在页面类方法中,这一行重试null。我不明白为什么?

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

页面类中的方法。

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

            }

定位器类中的定位器

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

测试类中的测试用例

@Test
    public void verify_TopNav_links()
    {

        hpage.verify_topnav_links();
    }

我也不知道这种方法是否正确。谁能告诉我验证这种情况的最佳方法。

注意:这个zillow.com如果我们使用selenium多次打开,会开始显示验证码。为了忽略它,当测试用例被执行时,手动从url中删除catpcha参数。

selenium selenium-webdriver xpath automation pageobjects
1个回答
1
投票

看来你需要使用

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

此处

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

这个xpath

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

似乎包含多个元素。但你是想直接访问href属性。这个 li 这里没有href属性。

Screenshot

© www.soinside.com 2019 - 2024. All rights reserved.