2011-12-07 13 views
0

我的要求是,我想搜索google和結果頁上的文本,找到所有鏈接其中有文本,稍後點擊該鏈接。步驟的要求 描述: 第一步)打開谷歌主頁 步驟2)上的搜索結果頁打印所有包含搜索關鍵詞 第四步:鏈接的關鍵詞 步驟3))點擊鏈接包含搜索關鍵詞selenium rc打開一個Google搜索頁面並搜索文本並打印出包含該文本的所有網址

所以想知道有沒有什麼辦法由硒或任何其他工具來實現這一目標,詳細描述讚賞提前

感謝

回答

1

這裏的它使用硒的webdriver一個C#控制檯應用程序。

using System; 
using System.Collections.Generic; 
using System.Threading; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Firefox; 

namespace ConsoleApplication 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      IWebDriver driver = new FirefoxDriver(); 

      // The keyword you would like to search for 
      string keyword = "stack overflow"; 

      // Step 1 
      driver.Url = "http://www.google.com/"; 

      // Step 2 
      IWebElement input = driver.FindElement(By.Id("lst-ib")); 
      input.SendKeys(keyword); 
      input.Submit(); 

      // Wait for page to load 
      Thread.Sleep(3000); 

      // Fill a list with the resulting links 
      List<string> results = new List<string>(); 
      ICollection<IWebElement> searchResults = driver.FindElements(By.XPath("//ol[@id='rso']/li/div/h3/a")); 
      foreach (IWebElement resultLink in searchResults) 
      { 
       string link = resultLink.GetAttribute("href"); 
       results.Add(link); 
      } 

      // Output each link and click on each link 
      foreach (string link in results) 
      { 
       // Step 3 
       Console.WriteLine(link); 

       // Step 4 
       driver.Url = link; 
      } 

      Console.ReadLine(); 
     } 
    } 
} 
+0

感謝您的解決方案,但我想這樣做在Java中,所以如果你解釋如何填寫清單,在Java中得到的鏈接,因爲我很新的編程語言,這將是非常有益的。 – Mitul

+0

這不是你問的問題...在Java中你可以使用ArrayList。 – shamp00

+0

對不起,我已經使用數組完成了它!謝謝您的幫助 – Mitul

0

此代碼將搜索google中的文本並收集結果頁面上的所有鏈接!

package com.example.tests; 
    import java.io.FileWriter; 
    import java.io.IOException; 
    import java.util.ArrayList; 
    import java.util.List; 
    import java.util.concurrent.TimeUnit; 
    import java.util.regex.Matcher; 
    import java.util.regex.Pattern; 

    import org.apache.commons.lang.builder.ToStringStyle; 
    import org.openqa.selenium.By; 
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.WebElement; 
    import org.openqa.selenium.firefox.FirefoxDriver; 

    import com.thoughtworks.selenium.*; 
    public class CopyOfsource { 
     private static WebDriver driver; 
     public static void main(String[] args) throws IOException { 


      driver = new FirefoxDriver(); 
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
      System.out.print("***********test**************"); 
      driver.get("http://www.google.co.in/#hl=en&q=test&oq=test&aq=f&aqi=g10&aql=1&gs_sm=e&gs_upl=1452727l1453164l0l1453839l7l5l0l0l0l0l278l1194l2- 

    5l5l0&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=2bcac73b2935e785&biw=1366&bih=643"); 

      List<WebElement> element = driver.findElements(By.xpath("//ol[@id='rso']/li/div/h3/a")); 

      List<String> results = new ArrayList<String>(); 

      int num=element.size(); 
      System.out.println("***************Report Created is in Location "+ num + "/n"); 
      for(int i=0;i<num;i++) 
      { 
       WebElement resultlink = element.get(i); 
       String link = resultlink.getAttribute("href"); 
       results.add(link); 
       System.out.println("***************Report Created is in Location : " + link); 

      } 




      driver.close(); 



     }} 
相關問題