0

Everyone. This question maybe duplicated with Using PhantomJs, how to get and handle the new page?. But there was not exact answer.

My question is as follows. The pages what I want to scrape 3 pages. 1st Page : input unique id , click next. if valid id => go to 2nd page. 2nd Page : click link(contains id) 3rd Page : download Pdf File.

So my aim is to download pdf file from unique id automatically.

Then the main point is how to handle page transition in Phantom PHP?

My test Code is as:

        // Use the composer autoloader.
        require_once 'vendor/autoload.php';
        // Setup mink drivers.
        $goutteDriver = new \Behat\Mink\Driver\GoutteDriver();
        $phantomjsDriver = new \Behat\Mink\Driver\Selenium2Driver('phantomJS');

        // Setup mink sessions.
        $goutteSession = new \Behat\Mink\Session($goutteDriver);
        $phantomjsSession = new \Behat\Mink\Session($phantomjsDriver);

        // Setup mink session manager.
        $mink = new \Behat\Mink\Mink();

        // Register sessions.
        $mink->registerSession('goutte', $goutteSession);
        $mink->registerSession('phantomjs', $phantomjsSession);

        // Set Goutte as the default session.
        $mink->setDefaultSessionName('phantomjs');
        

        // Visit mink website with phantomjs driver.

        $mink->getSession('phantomjs')->visit('https://testurl.com');
        
        // Get the default goutte session.
        $session = $mink->getSession('phantomjs');

        // Get the page document.
        $page = $session->getPage();
        echo $session->getCurrentUrl(), PHP_EOL;

        // $page->find('css', '#guides')->clickLink("Drivers");
        // echo $session->getCurrentUrl(), PHP_EOL;

        // Output the installation instructions from the page.
        $input = $page->find('css', '#id');
        $input->setValue("1234567890");
        echo $input->getValue(), PHP_EOL;
        $page->find('css', '#validar')->Click();
        echo $page->find('css', '#validar')->getValue(), PHP_EOL;

        $session->executeScript('document.getElementById("validar").click()');
        //$session->reload();
        sleep(5);
//      $mink->getSession('phantomjs')->visit('https://testurl.com/next');
        $page = $session->getPage();
        echo $session->getCurrentUrl(), PHP_EOL;
        
        sleep(5);
        $mink->getSession('phantomjs')->visit('https://testurl.com/next2?id=1234567890');
        $page = $session->getPage();
        echo $session->getCurrentUrl(), PHP_EOL;

        // Stop browser sessions.
        $mink->stopSessions();

So How to handle page transition? How to download pdf file properly?

0 Answers0