2014-11-24 80 views
0

逐個單擊這些鏈接一個我要做到以下幾點:如何獲取所有鏈接,並使用webdriver的

  1. 我想獲取並顯示在網頁上所有鏈接。
  2. 顯示後,我想單擊每個鏈接。

我能夠做點1使用foreach循環,但我無法第二點。

下面是代碼:

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

回答

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 
} 
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("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); 
    } 
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) 
     {    
     }  
    } 
相關問題