Selenium FF WebDriver 遍曆所有連結(另類爬蟲)

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

請看這個頁面,我想要找到某個公告的內容,必須一個一個開啟連結,尼瑪好多啊。

於是,我機智的使用selenium開啟每一個連結,然後把公告內容寫入txt

那需要做一下步奏

1.依次開啟一個公告

2.切換focus到新視窗,找到公告內容,寫到txt

3.關閉該視窗

4.切換到主視窗

5.當前頁面遍曆完,點擊下一頁

6.重複步奏1

 

由於下一頁是一個很好用的flag,就可以當做迴圈條件,因為最後一頁沒有下一頁的element

接下來要找到相關的的xpath

列表數目: count(//tr/td/a[starts-with(@href,‘article_show.asp?ID=‘) and @title!=‘‘ ])
列表:      //tr/td/a[starts-with(@href,‘article_show.asp?ID=‘) and @title!=‘‘ ]
下一頁:   //div/a[text()=‘下一頁‘]

selenium WebDriver測試網頁時,點擊target=”_blank”的連結,在開啟新頁面,切換到新視窗的

這要使用

String currentWindow = driver.getWindowHandle();//擷取當前視窗控制代碼
Set<String> handles = driver.getWindowHandles();//擷取所有視窗句

WebDriver window = driver.switchTo().window(it.next());//切換到新視窗

driver.switchTo().window(currentWindow);//回到原來頁面

driver=driver.switchTo().window(driver.getWindowHandle()); //把下一頁變成當前driver

currentWindow = driver.getWindowHandle();            //get all windows            Set<String> handles= driver.getWindowHandles();            for (String s : handles)            {                //current page is don‘t close                if (s.equals(currentWindow))                    continue;                else                 {                    window =driver.switchTo().window(s);                                }                             window .close() ;                        }driver.switchTo().window(currentWindow);
View Code

具體代碼

 

package com.packt.webdriver.chapter3;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.util.List;import java.util.Set;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;public class TraversalAllLinks {    private static String currentWindow;    public static void main(String[] args) {             WebDriver driver=DriverFactory.getFirefoxDriver();        driver.get("http://www.lhgtj.gov.cn/article.asp?ClassID=86&page=1");            driver.manage().window().maximize();        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);        driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);        WebElement nextPage=driver.findElement(By.xpath("//tr/td/a[@title=‘下一頁‘]"));        while(nextPage.isDisplayed())        {            List<WebElement> links=driver.findElements(By.xpath("//tr/td/a[starts-with(@href,‘article_show.asp?ID=‘) and @title!=‘‘ ]"));                for(WebElement link:links)        {            WebDriver window;            System.out.println(link.getText());            try {                writeToTXT(link.getText());            } catch (IOException e1) {                // TODO Auto-generated catch block                e1.printStackTrace();            }            link.click();            currentWindow = driver.getWindowHandle();            //get all windows            Set<String> handles= driver.getWindowHandles();            for (String s : handles)            {                //current page is don‘t close                if (s.equals(currentWindow))                    continue;                else                 {                    window =driver.switchTo().window(s);                    window.manage().window().maximize();                    window.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);                    window.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);                    //get all tables                    List<WebElement> tbs=window.findElements(By.xpath("//tbody/tr/td/p"));                    for(WebElement tb:tbs)                    {                        System.out.println(tb.getText());                        try {                            writeToTXT(tb.getText()+"\n");                        } catch (IOException e) {                            // TODO Auto-generated catch block                            e.printStackTrace();                        }                                         }                    //close the table window                    window .close() ;                }            //swich to current window            driver.switchTo().window(currentWindow);          }                        }        // click next page        nextPage.click();        //set next page to current page        driver=driver.switchTo().window(driver.getWindowHandle());        driver.manage().window().maximize();        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);        driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);        nextPage=driver.findElement(By.xpath("//tr/td/a[@title=‘下一頁‘]"));                }            }    //write logs    public static void  writeToTXT(String message) throws IOException    {        BufferedWriter bf = null;        try {            //set true ,avoid             bf = new BufferedWriter(new FileWriter("report.txt", true));            bf.write(message);            bf.flush();                   } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        finally        {             bf.close();        }        }}

DriverFactory

public static WebDriver getFirefoxDriver()    {        try        {            WindowsUtils.tryToKillByName("firefox.exe");        }        catch(Exception e)        {            System.out.println("can not find firefox process");        }        File file=new File("d:\\firebug-2.0.4-fx.xpi");        FirefoxProfile profile = new FirefoxProfile();          try {            profile.addExtension(file);            profile.setPreference("extensions.firebug.currentVersion", "2.0.4");            profile.setPreference("extensions.firebug.allPagesActivation", "on");        } catch (IOException e3) {            // TODO Auto-generated catch block            e3.printStackTrace();        }             WebDriver driver = new FirefoxDriver(profile);        return driver;            }
View Code

 

Selenium FF WebDriver 遍曆所有連結(另類爬蟲)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.