Advertisement
HelloWar75

Untitled

Apr 13th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. const puppeteer = require('puppeteer');
  2.  
  3. function getDateString(date, separator = '/') {
  4. var d = ('0' + date.getDate()).slice(-2)
  5. var m = ('0' + (date.getMonth() + 1)).slice(-2)
  6. var y = date.getFullYear()
  7. return m + separator + d + separator + y
  8. }
  9.  
  10. async function search(orr, dest, date)
  11. {
  12. const browser = await puppeteer.launch()
  13. function timeout(ms) {
  14. return new Promise(resolve => setTimeout(resolve, ms));
  15. }
  16. const page = await browser.newPage()
  17. await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36')
  18.  
  19. await page.goto('http://united.com/ual/en/us/flight-search/book-a-flight/', {waitUntil: 'networkidle0'})
  20.  
  21. page.on('response', async msg => {
  22. if (msg.request().resourceType() == 'xhr') {
  23. if (msg.request().url() == 'https://www.united.com/ual/en/us/flight-search/book-a-flight/flightshopping/getflightresults/awd') {
  24. try {
  25. const data = await msg.json()
  26. console.log("ACHEI O JSON AWD!")
  27. }
  28. catch (error) {}
  29. }
  30. }
  31. })
  32.  
  33. let results = null
  34. let ready = false
  35.  
  36. let lang = await page.$("a.language-region-change.btn.btn-primary");
  37.  
  38. if (lang != null) {
  39. await page.click("a.language-region-change.btn.btn-primary", {delay: 100})
  40. }
  41.  
  42. await page.evaluate(() => {
  43. const ow = document.querySelector("#TripTypes_ow")
  44. ow.click()
  45. document.querySelector("#Trips_0__NonStop").click()
  46. document.querySelector("#Trips_0__OneStop").click()
  47. document.querySelector("#Trips_0__TwoPlusStop").click()
  48. })
  49. await page.click('#TripTypes_ow')
  50. await timeout(100)
  51. await page.evaluate(() => {
  52. const rm = document.querySelector("#RedeemMiles_rMiles")
  53. rm.click()
  54. })
  55. await timeout(100)
  56. await page.evaluate(function() {
  57. document.querySelector('#Trips_0__Origin').value = ''
  58. document.querySelector('#Trips_0__Destination').value = ''
  59. document.querySelector('#Trips_0__DepartDate').value = ''
  60. })
  61. await timeout(100)
  62. await page.type('#Trips_0__Origin', orr, {delay: 100})
  63. await timeout(100)
  64. await page.type('#Trips_0__Destination', dest, {delay: 100})
  65. await timeout(100)
  66. await page.type('#Trips_0__DepartDate', date, {delay: 100})
  67. await timeout(1000)
  68. await page.evaluate(() => {
  69. const btn = document.querySelector("#btn-search")
  70. btn.click()
  71. })
  72.  
  73. page.screenshot({path: 'example.png', fullPage: true})
  74. }
  75.  
  76. search("PDX", "EWR", "5/15/18");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement