如何验证 url 是否已重定向到 Selenium 中的相应页面?

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

我正在尝试自动化 - 验证 https://www.zillow.com 的顶级导航链接。

为此,我将实际的 url 与预期的 url 相匹配。但是在 page class 方法中,这一行返回 null。我不明白为什么?

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

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

            }

定位器中的定位器class:

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

测试中的测试用例class:

@Test
    public void verify_TopNav_links()
    {

        hpage.verify_topnav_links();
    }

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

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

看来您需要使用

//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 属性。