简体   繁体   中英

Using Java Selenium to scrape a data across multiple pages when the url doesn't change

在此处输入图片说明 I have been trying to write a simple code to scrape some basic stats from www.whoscored.com. My code loops through first page links and scrape data just fine but when I switch with driver.findElement(By.xpath("")).click(); to next table, it collects data from first game BUT then keeps looping/return AGAIN on the first table/page...

Here is the code:

package Whoscored;

import java.io.File;


public class Datascrape {

public static void main(String[] args) throws InterruptedException, 
IOException {


        WebDriver driver = new FirefoxDriver();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)

              // get the URL               
               driver.get("https://www.whoscored.com/Regions/81/Tournaments/6/Seasons/6393/Germany-Bundesliga-II");

        Thread.sleep(1000);
        driver.manage().window().maximize();

          // first loop to identify matches-list over and over again

         for (int w=0;w<=100;w++){

        List<WebElement> lista1 = 
        driver.findElements(By.cssSelector(".match-link.match-report.rc"));

          // second loop for each table

        for (int j=0;j<=lista1.size(); j++){
        lista1.get(j).click();
        driver.manage().window().maximize();


// go deeper (two clicks), grab stats and return to original position/table

   // clicks
   driver.findElement(By.xpath(".//*[@id='sub-
   navigation']/ul/li[4]/a")).click();  
   driver.findElement(By.xpath(".//*[@id='live-match-
   options']/li[3]")).click();

   // grab stats/text       
   String home6 = driver.findElement(By.xpath(".//*
   [@id='chalkboard']/div[2]/div[1]/div[3]/div[2]/span[1]")).getText();
   String away6 = driver.findElement(By.xpath(".//*
   [@id='chalkboard']/div[2]/div[1]/div[3]/div[2]/span[2]")).getText();  

          driver.navigate().back();
          Thread.sleep(5000);
          driver.navigate().back();
          Thread.sleep(5000); 
   lista1= driver.findElements(By.cssSelector(".match-link.match-
   report.rc"));

            }
   // after it finishes with first table go to second table with click

   driver.findElement(By.xpath(".//*[@id='date-
   controller']/a[1]/span")).click();
   Thread.sleep(5000);
    }   
           }
}

You have to make sure that data has been loaded before reading the document. eg your call

driver.findElement(By.xpath(".//*[@id='date-controller']a[1]/span")).click();

is fine, but you also need to make sure that the next sibling

driver.findElement(By.xpath(".//*[@id='date-controller']a[2]/span"))

has changed its value for example the initial span contains "23 - 29 Jan 2017" and onclick it should change to "19 - 25 Dec 2016"

after a click to //*[@id="date-controller"]/a[3]/span you need wait reload element:

ExpectedConditions.stalenessOf(....))

You can use NoraUi if your program gets bigger and more complex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM