Giter VIP home page Giter VIP logo

Comments (11)

WallyJ avatar WallyJ commented on May 12, 2024 8

Instead of a simple critique of my PHP, you could have plainly seen that any mistakes that existed were correct syntax for Javascript, not just poor PHP. Seems like you treated my code as PHP written from scratch. The point is to translate the code. This was an opportunity for you to teach me a few translations rather than simply pointing out flaws. I will work on my PHP skills. Please work on your documentation. Also, I asked if you knew of any resources for translating the code, as this is not something that is done regularly, mainly because much of the code has to remain Javascript using JsFunction. Also, I can't "Ask" at stackoverflow about your library, since you wrote it. Everyone there would tell me to ask you.

from puphpeteer.

nesk avatar nesk commented on May 12, 2024 1

To convert JavaScript objects to PHP's syntax, you must be write them as associative arrays. So the following JS object:

{headless: false}

will be written like this in PHP:

['headless' => false]

This should solve your syntax issues. 🙂

from puphpeteer.

nesk avatar nesk commented on May 12, 2024 1

While I agree this project could benefit from a better a documentation, you seem to lack some common knowledge in PHP, maybe you should dig a bit into this language before using any kind of library.

For example, here are some of your errors.

  • You declare a variable and use it later as a constant:
$USERNAME_SELECTOR = '#SignInEmail';
// ...
$page->click(USERNAME_SELECTOR);
  • You try to use an unknown of keyword and you forgot the $ character before the variable names:
foreach (id of ids) {
    // ...
}
  • You're trying to concatenate strings by using the + operator instead of .:
$mainlink = "https://www.website.com/find/?action=details&query=" + id + "&orgid=obdra-n&unitid=" + id;
  • etc…

I cannot help you more with your current knowledge, I highly suggest you to learn to code in PHP if you want to use my library. Good luck! 🙂

from puphpeteer.

mkschaller avatar mkschaller commented on May 12, 2024 1

To convert JavaScript objects to PHP's syntax, you must be write them as associative arrays. So the following JS object:

{headless: false}

will be written like this in PHP:

['headless' => false]

This should solve your syntax issues. 🙂

I've been searching for this option all day, thanks!

from puphpeteer.

WallyJ avatar WallyJ commented on May 12, 2024

Thanks for the answer! No more syntax error on that line, but I'm running into a lot of other syntax errors. It is probably because I don't translate javascript into PHP very well. Is there a resource you can point me to that will help? Thanks!

from puphpeteer.

nesk avatar nesk commented on May 12, 2024

Show me your code, I can show you your mistakes and you will learn from that.

from puphpeteer.

WallyJ avatar WallyJ commented on May 12, 2024

from puphpeteer.

nesk avatar nesk commented on May 12, 2024

I would prefer if you paste your code here and strip the confidential parts (like the urls and selectors).

from puphpeteer.

WallyJ avatar WallyJ commented on May 12, 2024

Below is my code that is currently working in Puppeteer when I run the JS file via the terminal:

const CREDS = require('./creds');
const puppeteer = require('puppeteer');
puppeteer.launch({headless: false}).then(async browser => {
const page = await browser.newPage();
await page.goto('https://www.website.com/');
const USERNAME_SELECTOR = '#SignInEmail';
const PASSWORD_SELECTOR = '#SignInPassword';
const BUTTON_SELECTOR = '#SignInBtn';

await page.click(USERNAME_SELECTOR);
await page.keyboard.type(CREDS.username);

await page.click(PASSWORD_SELECTOR);
await page.keyboard.type(CREDS.password);

await page.click(BUTTON_SELECTOR);

await page.waitForNavigation();

//Add array of values for links
var ids = ["090", "660", "971"];
//Set variable for array of Values
var valarray = [];
var valarrayfinal = [];

for (let id of ids) {
  //Put variable into link:
  var mainlink = "https://www.website.com/find/?action=details&query=" + id + "&orgid=obdra-n&unitid=" + id;
  
  //Print link to the console
  console.log(mainlink);

  //Wait for the page to load

  await page.goto(mainlink);
    
  //Obtain the mainnumber
  // if (await page.$('body > div.siteContainer > main > div.siteContentContainer > div > div > div > div.dlColumns.clearfix > div.dlLeftColumn > div.number.numberBlue') !== null) {

  // }
  const MAINVALUE = await page.evaluate(() => document.querySelector('body > div.siteContainer > main > div.siteContentContainer > div > div > div > div.dlColumns.clearfix > div.dlLeftColumn > section.sumBasicFactsAndPhotos > div.sumBasicFacts > div:nth-child(1) > div:nth-child(2) > div.price.priceGray').textContent);
  
  // else const MAINVALUE = null;
  if (!MAINVALUE) (
    return null;
    valarray.push(null);
  }
  
  else {
    
    //Obtain Class List
    const myvaluearray = await page.evaluate( () => Array.from( document.querySelector( '.valScore' ).classList ) );
            
    //Pull 3rd value from the class list
    const myvalue = myvaluearray[2];

    //Pull last character from the single class
    const finalvalue = myvalue.substr(myvalue.length - 1);
    
    //Add val value to array
    valarray.push(finalvalue);
  }
    
  

  //Print values to the console
  console.log(MAINVALUE);
  console.log(myvalue);
  console.log (finalvalue);
  };
  

//Add comma separator to val Value Array
valarrayfinal = valarray.join();

//Print val Value Array to the console
console.log(valarrayfinal);

//Close the browser when finished
browser.close(); });

Currently it opens a browser, finds values, concatenates certain values and posts them all to the console since I am testing the process. Ultimately I will store these values in an array, or individual values in a variable to be stored in a DB.

Thanks again for your help with translating my code. One of the reasons I want to run Puppeteer in PHP is to have the variables/arrays in my project available before, during, and after the Puppeteer work without having to use AJAX to go back and forth between PHP variables, Javascript variables, and the DB.

from puphpeteer.

nesk avatar nesk commented on May 12, 2024

Well, I was expecting you to show me your PHP code, not the JavaScript one. I can take a bit of time to help you correct your mistakes, but I'm not willing to translate all of your code by myself, otherwise you should head over to Stack Overflow for this kind of request.

from puphpeteer.

WallyJ avatar WallyJ commented on May 12, 2024

I understand. However, respectfully, this project feels like an API with extremely minimal instructions, or the simply fact is that I don't know both languages enough to translate from one to the other. I would respectfully ask you to tell me which one is the case. I read the documentation on JsFunctions, but didn't see any examples referencing document.querySelector which is one of the most used functions in Puppeteer projects.

Below is the code as best as I have translated it. Your help is appreciated:

require 'creds.php';
$puppeteer = new Puppeteer;
$browser = $puppeteer->launch(['headless' => false]);
$page = $browser->newPage();
$page->goto('https://www.website.com/');
$USERNAME_SELECTOR = '#SignInEmail';
$PASSWORD_SELECTOR = '#SignInPassword';
$BUTTON_SELECTOR = '#SignInBtn';
    
$page->click(USERNAME_SELECTOR);
$page->keyboard->type(CREDS.username);

$page->click(PASSWORD_SELECTOR);
$page->keyboard->type(CREDS.password);

$page->click(BUTTON_SELECTOR);

$page->waitForNavigation();

    //Add array of values for links
    $ids = ["090", "660", "971"];
    //Set variable for array of Values
    $valarray = [];
    $valarrayfinal = [];

    foreach (id of ids) {
      //Put variable into link:
      $mainlink = "https://www.website.com/find/?action=details&query=" + id + "&orgid=obdra-n&unitid=" + id;
      
      //Print link to the console
      console.log(mainlink);

      //Wait for the page to load

      $page->goto(mainlink);
        
      //Obtain the mainnumber
      // if (await page.$('body > div.siteContainer > main > div.siteContentContainer > div > div > div > div.dlColumns.clearfix > div.dlLeftColumn > div.number.numberBlue') !== null) {

      // }
      $MAINVALUE = $page->evaluate(JsFunction::createWithParameters(['element'])
      ->body("return
      document.querySelector('body > div.siteContainer > main > div.siteContentContainer > div > div > div > div.dlColumns.clearfix > div.dlLeftColumn > section.sumBasicFactsAndPhotos > div.sumBasicFacts > div:nth-child(1) > div:nth-child(2) > div.price.priceGray').textContent
      ";
      
      // else const MAINVALUE = null;
      if (!MAINVALUE) (
        return null;
        array_push(null);
      }
      
      else {
        
        //Obtain Class List
        $myvaluearray = await page.evaluate( () => Array.from( document.querySelector( '.valScore' ).classList ) );
                
        //Pull 3rd value from the class list
        $myvalue = myvaluearray[2];

        //Pull last character from the single class
        $finalvalue = myvalue.substr(myvalue.length - 1);
        
        //Add val value to array
        array_push($valarray, $finalvalue);
      }
        
      

      //Print values to the console
      scope('mainvalue' => $MAINVALUE)->body("console.log(mainvalue)");
      scope('myvalue' => $myvalue)->body("console.log(myvalue)");
      scope('finalvalue' => $finalvalue)->body("console.log(finalvalue)");
      };
      
    
    //Add comma separator to val Value Array
    $valarrayfinal = implode(', ', $valarray);
    
    //Print val Value Array to the console
    scope('arrayfinalvalue' => $valarrayfinal)->body("console.log(valarrayfinal)");
    
    //Close the browser when finished
    $browser->close();
  });

from puphpeteer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.