From 1993e0b049c4c3e8084c747057ae7df80390f77d Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Wed, 23 Dec 2020 22:58:47 +0100 Subject: [PATCH] set loading strategy explicitly --- README.md | 3 +++ nzz.js | 20 ++++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 717297c..9f46795 100644 --- a/README.md +++ b/README.md @@ -53,5 +53,8 @@ directory "./nzz" You need a good internet connection, as the program only waits 5 seconds until a download of an issue can start. This is something that is hard to solve unfortunately. +If you get strange errors about elements not being visible, wait a bit and try again, +it's usually a network problem. + ## Licence Licenced as [MPL 2.0](https://www.mozilla.org/en-US/MPL/2.0/). diff --git a/nzz.js b/nzz.js index 26a63ca..52d1c5a 100755 --- a/nzz.js +++ b/nzz.js @@ -8,7 +8,7 @@ const tmp = require('tmp'); var path = require('path'); const fs = require('fs'); -const { Builder, By, Key, until } = require('selenium-webdriver'); +const { Builder, By, Capabilities, Key, until } = require('selenium-webdriver'); const fx = require('selenium-webdriver/firefox'); const yargs = require('yargs/yargs') const { hideBin } = require('yargs/helpers'); @@ -59,7 +59,7 @@ async function moveDownload(tmpDir, outDir, date) { } let srcFile = path.join(tmpDir, `Gesamtausgabe_NZZ_-_Neue_Zürcher_Zeitung_${date.isoDate()}.pdf`); - + fs.watchFile(srcFile, () => { if (fs.existsSync(srcFile)) { @@ -67,10 +67,10 @@ async function moveDownload(tmpDir, outDir, date) { if (needsOffset) { date = date.addDays(1); } - + let destFile = path.join(outDir, `${date.isoDate()}.pdf`); fs.copyFileSync(srcFile, destFile); - + resolve(); } }); @@ -86,11 +86,11 @@ async function moveDownload(tmpDir, outDir, date) { async function enterDate(driver, date) { let dateString = date.nzzDate(); - let startDate = await driver.wait(until.elementLocated(By.css('.fup-s-date-start')), WAIT_TIMEOUT, TIMEOUT_MSG, RETRY_DELAY); + let startDate = await driver.wait(until.elementLocated(By.css('input.fup-s-date-start')), WAIT_TIMEOUT, TIMEOUT_MSG, RETRY_DELAY); await startDate.clear(); await startDate.sendKeys(dateString); - let endDate = await driver.wait(until.elementLocated(By.css('.fup-s-date-end')), WAIT_TIMEOUT, TIMEOUT_MSG, RETRY_DELAY); + let endDate = await driver.wait(until.elementLocated(By.css('input.fup-s-date-end')), WAIT_TIMEOUT, TIMEOUT_MSG, RETRY_DELAY); await endDate.clear(); await endDate.sendKeys(dateString + Key.ENTER); } @@ -155,8 +155,8 @@ async function findIssues(driver, from, to, tmpDir, outDir) { try { let articles = await driver.wait(until.elementsLocated(By.css('.fup-archive-result-item-article-title')), SEARCH_WAIT_TIMEOUT, SEARCH_TIMEOUT_MSG, RETRY_DELAY); - await articles[0].click();s - + await articles[0].click(); + await download(driver); console.log(`downloading ${from.isoDate()}...`); @@ -198,7 +198,11 @@ async function run(from, to, user, password, outDir) { .setPreference('browser.download.dir', tmpDir.name) .setPreference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf'); + const caps = new Capabilities(); + caps.setPageLoadStrategy("normal"); + let driver = await new Builder() + .withCapabilities(caps) .forBrowser('firefox') .setFirefoxOptions(fxOptions) .build();