如何排除已经验证的链接验证?

时间:2018-05-07 20:27:29

标签: java selenium

在脚本下面我必须验证页面上的链接。这是扭曲,我需要验证此页面中的链接,还需要单击每个链接,然后验证该页面上的链接,但我需要排除在第一页中验证的链接。我真的不知道该怎么做。我可以点击该链接并验证该页面中的链接,但我应使用哪些代码来排除那些已经过验证的代码。如果可以的话请帮忙。感谢

package siteLinks;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class LinksValidation {
    private static WebDriver driver = null;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String homePage = "http://www.safeway.com/Shopstores/Site-Map.page";
        String url = "http://www.safeway.com/Shopstores/Site-Map.page";
        HttpURLConnection huc = null;
        int respCode = 200;

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\aaarb00\\Desktop\\Quotients\\lib\\chromedriver.exe");

        driver = new ChromeDriver();

        driver.manage().window().maximize();

        driver.get(homePage);

        List<WebElement> links = driver.findElements(By.tagName("a"));

        Iterator<WebElement> it = links.iterator();

        while(it.hasNext()){

            url = it.next().getAttribute("href");

            System.out.println(url);

            if(url == null || url.isEmpty()){
                System.out.println("URL is either not configured for anchor tag or it is empty");
                continue;
            }

            try {
                huc = (HttpURLConnection)(new URL(url).openConnection());

                huc.setRequestMethod("HEAD");

                huc.connect();

                respCode = huc.getResponseCode();

                if(respCode >= 400){
                    System.out.println(url+" is a broken link");
                }
                else{
                    System.out.println(url+" is a valid link");
                }

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        driver.quit();

    }
}

1 个答案:

答案 0 :(得分:1)

您可以将已经访问过的链接存储在ArrayList中,并检查该ArrayList是否已包含该链接。

  socket: {
    domain: 'example.com'
  }

我不确定您是如何检查您检查状态200 OK响应的页面的链接,但您应该定义要检查链接的每个页面的URL然后遍历这些URL。否则,您可能会退出网站,检查链接并转出到更广泛的互联网上。在那种情况下,你的测试可能永远不会完成。