Веб-записка Selenium не загружается при загрузке Spring - PullRequest
0 голосов
/ 11 июля 2020

Я хочу отобразить таблицу данных в веб-приложении с весенней загрузкой, и в процессе очистки данных страница застряла при загрузке с непрерывной загрузкой значка круга. Я предполагаю, что данные слишком велики для обработки или что-то не так с моим кодом. Посоветуйте, пожалуйста, способ обработки данных и их загрузки.

Сообщения отладки

    2020-07-11 14:31:30.874 TRACE 20112 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/index", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
    2020-07-11 14:31:30.877 TRACE 20112 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance of singleton bean 'indexController'
    2020-07-11 14:31:30.877 TRACE 20112 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.covid.tracker.indexController#printTable()
    2020-07-11 14:31:30.884 TRACE 20112 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Arguments: []
    C:\Users\jinyi\Desktop\Spring\Coronavirus-Tracker\target\classes\chromedriver.exe
    Starting ChromeDriver 83.0.4103.39 (ccbf011cb2d2b19b506d844400483861342c20cd-refs/branch-heads/4103@{#416}) on port 29554
    Only local connections are allowed.
    Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
    ChromeDriver was started successfully.
    2020-07-11 14:31:44.302  INFO 20112 --- [ null to remote] o.o.selenium.remote.ProtocolHandshake    : Detected dialect: W3C

Контроллер

@RestController
public class indexController {

    @Autowired
    private WebScrap webscrap;
    
    @RequestMapping(value = "/index")
    public String printTable() {
        System.out.println("webscrap" + webscrap.getAllData());
        return "index";
    }   
}

Веб-записка

@Service
public class WebScrap {

    final static private String url = "https://www.worldometers.info/coronavirus/";
    
    private WebDriver load() {
        try {
        Resource resource = new ClassPathResource("chromedriver.exe");
        String filePath = resource.getFile().getPath();
        System.out.println(filePath);
        System.setProperty("webdriver.chrome.driver", filePath);
        ChromeOptions options = new ChromeOptions();
        options.setHeadless(true);
        WebDriver browser = new ChromeDriver(options);
        browser.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        browser.get(url);
        return browser;
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
    public ArrayList<Data>getAllData(){
        ArrayList<Data>allData = new ArrayList<Data>();
        try {
        Thread.sleep(10000);
        WebDriver browser = load();
        int row = browser.findElements(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr")).size();
        int col = browser.findElements(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td")).size();
        for ( int i = 3; i < row; i++) {
            for ( int j = 1; j < col; j++) {
        Data data = new Data();
        data.setId(browser.findElement(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr["+i+"]/td["+j+"]")).getText());
        data.setCountry(browser.findElement(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr["+i+"]/td["+j+"]")).getText());
        data.setTotalCases(browser.findElement(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr["+i+"]/td["+j+"]")).getText());
        data.setNewCases(browser.findElement(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr["+i+"]/td["+j+"]")).getText());
        data.setTotalDeaths(browser.findElement(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr["+i+"]/td["+j+"]")).getText());
        data.setNewDeaths(browser.findElement(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr["+i+"]/td["+j+"]")).getText());
        data.setTotalRecovered(browser.findElement(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr["+i+"]/td["+j+"]")).getText());
        data.setActiveCases(browser.findElement(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr["+i+"]/td["+j+"]")).getText());
        data.setSeriousCases(browser.findElement(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr["+i+"]/td["+j+"]")).getText());
        data.setTotalCasesPerMillionPop(browser.findElement(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr["+i+"]/td["+j+"]")).getText());
        data.setTotalDeathsPerMillionPop(browser.findElement(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr["+i+"]/td["+j+"]")).getText());
        data.setTotalTests(browser.findElement(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr["+i+"]/td["+j+"]")).getText());
        data.setTestsPerMillion(browser.findElement(By.xpath("/html[1]/body[1]/div[3]/div[3]/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr["+i+"]/td["+j+"]")).getText());       
        allData.add(data);
            }
        }
        browser.quit();
        browser.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    
        return allData;
        }
    
}
...