Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call to undefined method HeadlessChromium\Page::sendFile() #643

Open
richardsonoge opened this issue Jul 28, 2024 · 7 comments
Open

Call to undefined method HeadlessChromium\Page::sendFile() #643

richardsonoge opened this issue Jul 28, 2024 · 7 comments

Comments

@richardsonoge
Copy link

richardsonoge commented Jul 28, 2024

I'm trying to send a PDF file as input on a site and I had used this code:

<?php
require 'vendor/autoload.php';

use HeadlessChromium\BrowserFactory;

$browserFactory = new BrowserFactory();
$browser = $browserFactory->createBrowser();

// Open a new page
    $page = $browser->createPage();
    $page->navigate($websiteUrl)->waitForNavigation();

    // Send File in the input file.
    $page->mouse()->find('.ZdLswd', 2)->click();
    $page->sendFile('documents/con.pdf');

But the problem it give me an error, what to do please?

Here's my errors:

richardsonoge@richardsonoge-blooglet:/opt/lampp/htdocs/PdfTranslatorHeadless$ php googletranslate.php
PHP Fatal error:  Uncaught Error: Call to undefined method HeadlessChromium\Page::sendFile() in /opt/lampp/htdocs/PdfTranslatorHeadless/googletranslate.php:32                                                                                    
Stack trace:                                                                                                             
#0 {main}                                                                                                                
  thrown in /opt/lampp/htdocs/PdfTranslatorHeadless/googletranslate.php on line 32                                       
richardsonoge@richardsonoge-blooglet:/opt/lampp/htdocs/PdfTranslatorHeadless$

How can I fix this error?

@enricodias
Copy link
Member

This method only exists in the Node element. The mouse class doesn't return a node after clicking, this class only controls the real mouse just like a human would. To use the sendFiles method you will need to use the Dom class.

@richardsonoge
Copy link
Author

This method only exists in the Node element. The mouse class doesn't return a node after clicking, this class only controls the real mouse just like a human would. To use the sendFiles method you will need to use the Dom class.

But how can I use it? Can you please give me an example?

@richardsonoge
Copy link
Author

richardsonoge commented Jul 29, 2024

This method only exists in the Node element. The mouse class doesn't return a node after clicking, this class only controls the real mouse just like a human would. To use the sendFiles method you will need to use the Dom class.

Is this the way I should use it?

<?php
require 'vendor/autoload.php';

use HeadlessChromium\BrowserFactory;
use HeadlessChromium\Dom;

$browserFactory = new BrowserFactory();
$browser = $browserFactory->createBrowser();

// Open a new page
    $page = $browser->createPage();
    $page->navigate($websiteUrl)->waitForNavigation();

    // Send File in the input file.
    $page->mouse()->find('.ZdLswd', 2)->click();
    $page->sendFile('documents/con.pdf');

It gave the same error:

richardsonoge@richardsonoge-blooglet:/opt/lampp/htdocs/PdfTranslatorHeadless$ php googletranslate.php
PHP Fatal error:  Uncaught Error: Call to undefined method HeadlessChromium\Page::sendFile() in /opt/lampp/htdocs/PdfTranslatorHeadless/googletranslate.php:22                                                                                    
Stack trace:                                                                                                             
#0 {main}                                                                                                                
  thrown in /opt/lampp/htdocs/PdfTranslatorHeadless/googletranslate.php on line 22                                       
richardsonoge@richardsonoge-blooglet:/opt/lampp/htdocs/PdfTranslatorHeadless$ 

What do i do? Tell me more please....

@enricodias
Copy link
Member

Adding the Dom namespace to your code won't do anything, you are still using the mouse class instead of the dom class. There are examples on how to use this class in the readme: https://github.com/chrome-php/chrome?tab=readme-ov-file#interacting-with-dom

@richardsonoge
Copy link
Author

Adding the Dom namespace to your code won't do anything, you are still using the mouse class instead of the dom class. There are examples on how to use this class in the readme: https://github.com/chrome-php/chrome?tab=readme-ov-file#interacting-with-dom

I just did it and it keeps giving me another error.

Here's my code:

<?php
require 'vendor/autoload.php';

use HeadlessChromium\BrowserFactory;

$browserFactory = new BrowserFactory();
$browser = $browserFactory->createBrowser();

// Open a new page
    $page = $browser->createPage();
    $page->navigate($websiteUrl)->waitForNavigation();

    // Send File in the input file.
    $page->mouse()->find('.ZdLswd', 2)->click();
    $page->dom()->sendFile('documents/con.pdf');

Here's my errors:

richardsonoge@richardsonoge-blooglet:/opt/lampp/htdocs/PdfTranslatorHeadless$ php googletranslate.php
PHP Fatal error:  Uncaught HeadlessChromium\Exception\DomException: -32000 - Node is not a file input element in /opt/lampp/htdocs/PdfTranslatorHeadless/vendor/chrome-php/chrome/src/Dom/Node.php:214                                            
Stack trace:                                                                                                             
#0 /opt/lampp/htdocs/PdfTranslatorHeadless/vendor/chrome-php/chrome/src/Dom/Node.php(205): HeadlessChromium\Dom\Node->assertNotError()                                                                                                            
#1 /opt/lampp/htdocs/PdfTranslatorHeadless/vendor/chrome-php/chrome/src/Dom/Node.php(194): HeadlessChromium\Dom\Node->sendFiles()                                                                                                                 
#2 /opt/lampp/htdocs/PdfTranslatorHeadless/googletranslate.php(23): HeadlessChromium\Dom\Node->sendFile()                
#3 {main}                                                                                                                
  thrown in /opt/lampp/htdocs/PdfTranslatorHeadless/vendor/chrome-php/chrome/src/Dom/Node.php on line 214                
richardsonoge@richardsonoge-blooglet:/opt/lampp/htdocs/PdfTranslatorHeadless$

What do i do to fix them?

@enricodias
Copy link
Member

I think you lack the basic knowledge about programming in general to accomplish this.

You are still using the mouse class here:

$page->mouse()->find('.ZdLswd', 2)->click();

And when you try to use the dom, there is no node selected. You need to understand how the classes work. Trying random things like that won't work, you'll find another error each time. All the errors you sent here are not related to this lib, they are normal php errors that a dev should be able to easily understand and troubleshot.

I understand you are a beginner, but you probably picked up a project that is too complex for your capabilities. If you are struggling with simple things like this, soon you'll find issues way harder to solve. The css class .ZdLswd that you are trying to select for example, it's a name generated dynamically that will probably change. Google translate has probably a ton of protections to prevent automated softwares like the one you are trying to create, they have a translation api for that and it's a paid product.

@richardsonoge
Copy link
Author

richardsonoge commented Jul 29, 2024

This result works fine. I've corrected it, but when the file is uploaded to Google Translate, it doesn't display its size like Google Translate. It only displays “con.pdf 0 Byte”, whereas if I do it myself in Google Translate it displays “con.pdf 431.1Kb”. What can I do? Here's my code:

<?php
require 'vendor/autoload.php';

use HeadlessChromium\BrowserFactory;

$browserFactory = new BrowserFactory();
$browser = $browserFactory->createBrowser();

// Open a new page
    $page = $browser->createPage();
    $page->navigate($websiteUrl)->waitForNavigation();

   // Set the path to your PDF file
    $filePath = 'documents/con.pdf';  // Update this path

    // Send File in the input file of Google Translate.
    $elem = $page->dom()->querySelector('input[type="file"]');
    $elem->sendFile($filePath);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants