如何获取所有链接并使用Selenium WebDriver逐个单击这些链接

时间:2014-11-24 17:24:25

标签: java selenium-webdriver

我想做以下事情:

  1. 我想获取并显示网页上的所有链接。
  2. 显示后,我想逐个点击每个链接。
  3. 我能够使用foreach循环执行第1点但我无法获得第2点。

    以下是代码:

    public class OpenAllLinks {
        public static void main(String[] args) {
            WebDriver driver=new FirefoxDriver();
            driver.get("http://bing.com");
            List<WebElement> demovar = driver.findElements(By.tagName("a"));
            System.out.println(demovar.size());
    
            for (WebElement var : demovar) {
                System.out.println(var.getText()); // used to get text present between the anchor tags
                System.out.println(var.getAttribute("href"));
            }
    
            for (WebElement var : demovar) {
                var.click();
            }
        }
    }
    

3 个答案:

答案 0 :(得分:1)

单击第一个链接时,浏览器将加载相应的页面。因此,您在第一页中捕获的其他链接将无法使用。

如果目的是导航到每个链接的目标,那么存储目标位置并导航到它,就像这样

driver.get("<some site>");
List<WebElement> links=driver.findElements(By.tagName("a"))
ArrayList<String> targets = new ArrayList<String>();
//collect targets locations
for (WebElement link : links) {
     targets.add(link.getAttribute("href"));
}
for (WebElement target : targets) {
     driver.get(target);
     //do what is needed in the target
}

答案 1 :(得分:0)

之所以发生这种情况,是因为点击后链接会导航到新页面,而在该页面中找不到要点击的列表中的下一个元素。请尝试以下代码,该代码将导航到每个链接(我已使用上面的@deepak代码,并根据您的需要对其进行了相应修改)

 WebDriver driver=new FirefoxDriver();

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

 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

 driver.get("http://bing.com");

 List<WebElement> demovar=driver.findElements(By.tagName("a"));
 System.out.println(demovar.size());

 ArrayList<String> hrefs = new ArrayList<String>(); //List for storing all href values for 'a' tag

    for (WebElement var : demovar) {
        System.out.println(var.getText()); // used to get text present between the anchor tags
        System.out.println(var.getAttribute("href"));
        hrefs.add(var.getAttribute("href")); 
        System.out.println("*************************************");
    }

    //Navigating to each link
    int i=0;
    for (String href : hrefs) {
        driver.navigate().to(href);
        System.out.println((++i)+": navigated to URL with href: "+href);
        Thread.sleep(3000); // To check if the navigation is happening properly.
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    }

答案 2 :(得分:0)

static WebDriver driver=null;
public static void main(String[] args) throws IOException 
{     System.setProperty("webdriver.chrome.driver","D:\\softwaretesting\\broswer driver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();``
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        //driver.manage().window().maximize();
        driver.get("http://google.com/");      
        List<WebElement> links=driver.findElements(By.tagName("a"));            
        System.out.println("Total links are "+links.size());            
        for(int i=0;i<links.size();i++)
        {       
            WebElement ele= links.get(i);               
            String url=ele.getAttribute("href");                
            verifyLinkActive(url);          
        }        
    }       
    public static void verifyLinkActive(String linkUrl)
    {           try 
        {
           URL url = new URL(linkUrl);             
           HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();               
           httpURLConnect.setConnectTimeout(3000);             
           httpURLConnect.connect();               
           if(httpURLConnect.getResponseCode()==200)               {
               System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage());
               File src= (TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);     
               FileUtils.copyFile(src, new File("D://screenshort//Spiritualbridge//"+System.currentTimeMillis()+".png"));   
            }             if(httpURLConnect.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND)  
           {
               System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage() + " - "+ HttpURLConnection.HTTP_NOT_FOUND);
            }                
        }           catch (Exception e) 
        {              
        }       
    }