2016-01-05 80 views
-2

我想獲得鏈接名稱的所有鏈接出現在一個特定的網頁,但一些鏈接可見後,懸停在一個特定的鏈接,就像有這些鏈接和Selenium WebDriver之間的父子關係無法識別子鏈接的鏈接名稱。如何獲得鏈接名稱後,盤旋在一個特定的鏈接

是否有任何可能的方式獲得所有鏈接名稱,包括父母和孩子?

package test; 

import static org.junit.Assert.*; 

import java.io.IOException; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.List; 
import java.util.concurrent.TimeUnit; 

import mx4j.tools.remote.http.HTTPConnectionManager; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.ie.InternetExplorerDriver; 

public class SparshLink { 

    WebDriver driver; 

    @Before 
    public void setUp() throws Exception { 
     String basePath = System.getProperty("user.dir"); 
     String finalPath = basePath + "\\IEDriver\\IEDriverServer.exe"; 
     System.setProperty("webdriver.ie.driver", finalPath); 

     driver = new InternetExplorerDriver(); 
     driver.navigate().to("http://sparshv2/"); 
    } 

    @After 
    public void tearDown() throws Exception { 
     driver.quit(); 
    } 

    @Test 
    public void test() throws Exception { 
     List<WebElement> list; 
     list = driver.findElements(By.tagName("a")); 
     System.out.println("Number of links : " + list.size()); 

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

     for (int i = 0; i < list.size(); i++) { 
      System.out.println(i); 
      URL url = new URL(list.get(i).getAttribute("href")); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      if (connection.getResponseCode() != 404) { 
       System.out.println(list.get(i).getText() + "is working properly."); 
      } else { 
       System.out.println(list.get(i).getText() + "is not working properly."); 
      } 
      connection.disconnect(); 


     } 
    } 

} 

對不起,我不能分享應用程序背後的代碼。

回答