2011-12-07 1 views
0

私の要件は、私はGoogleと結果ページで、テキストを検索するすべてのリンクを検索するそのテキストが存在し、後でそのリンクをクリックする。段階における要件の 説明: ステップ1)オープンGoogleホームページ STEP2)キーワード STEP3検索)結果ページの印刷上の検索キーワード STEP4が含まれているすべてのリンク)リンクをクリックしてくださいので、検索キーワードセレンrcは、Googleの検索ページを開き、テキストを検索し、そのテキストが存在するすべてのURLを印刷する

が含まれていますセレンによって、これを達成するための任意の方法または他のツールがある知っていただきたいと思います、詳細な説明は、あらかじめ

答えて

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



     }} 
関連する問題