Created
February 19, 2020 10:13
-
-
Save nevzatalkan/f0178a3ec6faa34898d3432fae130d87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @name Download file / upload file | |
* | |
* @desc Find an image by class selector, downloads the image, saves it to disk and read it again. Use this together with a .fileUpload() method. | |
* | |
*/ | |
const puppeteer = require('puppeteer') | |
const fs = require('fs') | |
const path = require('path') | |
const { promisify } = require('util') | |
function dateToTimeStamp(myDate) { | |
myDate = myDate.split("-"); | |
var newDate = myDate[1] + "/" + myDate[0] + "/" + myDate[2]; | |
return new Date(newDate).getTime(); //will alert 1330210800000 | |
} | |
// Normalizing the text | |
function getText(linkText) { | |
linkText = linkText.replace(/\r\n|\r/g, "\n"); | |
linkText = linkText.replace(/\ +/g, " "); | |
// Replace with a space | |
var nbspPattern = new RegExp(String.fromCharCode(160), "g"); | |
return linkText.replace(nbspPattern, " "); | |
} | |
// find the link, by going over all links on the page | |
async function findButton(page, linkString) { | |
const links = await page.$$('button') | |
for (var i = 0; i < links.length; i++) { | |
let valueHandle = await links[i].getProperty('innerText'); | |
let linkText = await valueHandle.jsonValue(); | |
const text = getText(linkText); | |
if (linkString == text) { | |
console.log(linkString); | |
console.log(text); | |
console.log("Found"); | |
return links[i]; | |
} | |
} | |
return null; | |
} | |
// find the link, by going over all links on the page | |
async function findLink(page, linkString) { | |
const links = await page.$$('a') | |
for (var i = 0; i < links.length; i++) { | |
let valueHandle = await links[i].getProperty('innerText'); | |
let linkText = await valueHandle.jsonValue(); | |
const text = getText(linkText); | |
if (text.indexOf(linkString) >= 0) { | |
console.log("Found link"); | |
return links[i]; | |
} | |
} | |
return null; | |
} | |
const readFileAsync = promisify(fs.readFile) | |
const writeFileAsync = promisify(fs.writeFile); | |
(async() => { | |
const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] }); | |
const page = await browser.newPage(); | |
await page.setViewport({ width: 1200, height: 800 }); | |
await page.goto(`https://finance.yahoo.com/quote/GARAN.IS/history?period1=1576713600&period2=1582070400&interval=1d&filter=history&frequency=1d`); | |
let linkHandlers1 = await findButton(page, "I agree"); | |
if (linkHandlers1 !== null) { | |
await linkHandlers1.click(); | |
await page.waitForNavigation({ waitUntil: 'load' }) | |
console.log("clicked agree button"); | |
//await page.screenshot({ path: './image.jpg', type: 'jpeg' }); | |
await page.waitFor(4000); | |
await page._client.send('Page.setDownloadBehavior', { | |
behavior: 'allow', | |
downloadPath: './' | |
}); | |
const downloadLink = await findLink(page, "Download Data"); | |
await page.waitFor(1000); | |
if (downloadLink !== null) { | |
console.log(downloadLink); | |
await downloadLink.click(); | |
await page.waitFor(4000); | |
} | |
} | |
else { | |
//throw new Error("Link 1 not found"); | |
} | |
browser.close() | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment