symfony / panther

A browser testing and web crawling library for PHP and Symfony
MIT License
2.94k stars 222 forks source link

Is there a way to click button without form or link? #172

Closed aurelijusrozenas closed 5 years ago

aurelijusrozenas commented 5 years ago

Hi,

I have popup with button for JS, so no form or link. I could not find a way to make panther client to click it. Is this (for some reason) not implemented or should I use some other technique (load custom JS maybe).

Kocal commented 5 years ago

I'm not a Panther user, but I know there is the method Crawler#selectButton():

$button = $crawler->selectButton('the text in your button');
// $button is a Crawler instance

But the method Client#click() does not seems to accept a button, only links...

Instead, you can probably use the method Client#executeScript like this:

$client->executeScript("document.querySelector('button').click()");
aurelijusrozenas commented 5 years ago

@Kocal you, sir, are gold!

Gobmichet commented 5 years ago

Hi, Got the same problem but when using your code i have the following exception ::

StateElementReferenceException : stale element reference: element is not attached to the page document

I tried to wait for the button with the following code :

$client-> waitFor('.button');

$client->executeScript("document.querySelector('.button').click()");

but i can't get it to work... Any idea please ?

aurelijusrozenas commented 5 years ago

@Gobmichet I do not think this anything to do with this code, I'd suggest you try to google panther StateElementReferenceException or webdriver StateElementReferenceException. Maybe this would have some clues.

Gobmichet commented 5 years ago

Thanks, I'll have a look ^^ Can i ask u some advice about another problem i have with symfony/panther here please ? https://github.com/symfony/panther/issues/183

what's more, i had a look to the link u gave N tried a for() in order to "retry". but same error...

trbsi commented 4 years ago

Just in case someone needs help. I had a case where I had a form and a button. I filled a form and clicked on button. Then new page opened. I needed to get data from new page. This is how I did it:

$url = "https://someurl";
$crawler = $this->client->request('GET', $url); //$this->client is Symfony\Component\Panther\Client
$form = $crawler->filter('#Form1')->form();
$form->setValues([
      'someField' => 12183,
]);
//This is where I executed JS script to click on a button
$client->executeScript("document.querySelector('#btnSpecial').click()");
//wait for new element on new page to appear
$this->client->waitFor('#results');
//refresh crawler so you can crawl newly loaded page
$crawler = $client->refreshCrawler();
//now crawl new page
madurapa commented 2 years ago

$crawler = $client->refreshCrawler(); method really help when you dealing with form elements as well...