PHP Facebook Webdriver Click issues - php

I am very new to PHP and just got everything up and running. I am trying to self teach and I don't understand why my click function is failing. It sometimes will not process at all and other times I get the following error: https://www.screencast.com/t/ogZPogsLXjJ
<?php
require_once('vendor/autoload.php');
$host = 'http://localhost:4444/wd/hub';
$driver = RemoteWebDriver::create($host, DesiredCapabilities::firefox());
$driver->get('https://github.com');
$driver->findElement(WebDriverBy::xpath('/html/body/div[1]/header/div/div[2]/nav/ul/li[2]/a'))->click();
?>

I got it working:
$element = $driver->findElement(WebDriverBy::xpath('/html/body/div[1]/header/div/div[2]/nav/ul/li[2]/a'));
if ($element->isDisplayed()) {
$element->click();
}

Related

How to crawl page in PHP?

I get the error: "error code: 1020".
The page I'm trying to crawl for form data is: https://v2.gcchmc.org/medical-status-search/.
This is my code:
$initial = file_get_contents('https://v2.gcchmc.org/medical-status-search/');
$check = preg_replace('/.+?input type="hidden" name="csrfmiddlewaretoken" value="(.+?)".*/sim', '$1'. $initial);
print $check;
"error code: 1020" the page I am trying to crawl for form data is https://v2.gcchmc.org/medical-status-search/. Can you help me what's wrong in the code below.
The site is protected by cloudflare. You can bypass the cloudflare when you have javascript enabled, so through command line is not going to work. You can however automate this by using Puppeteer for example, which also is available in PHP. But you have to disable headless to make it work.
Installation
composer require nesk/puphpeteer
npm install #nesk/puphpeteer
The script (test.php)
use Nesk\Puphpeteer\Puppeteer;
require_once __DIR__ . "/vendor/autoload.php";
function getToken($content)
{
preg_match_all('/.+?input type="hidden" name="csrfmiddlewaretoken" value="(.+?)".*/sim', $content, $matches);
return $matches[1][0];
}
$puppeteer = new Puppeteer;
$browser = $puppeteer->launch(['headless'=>false]);
/**
* #var $page \Nesk\Puphpeteer\Resources\Page
*/
$page = $browser->newPage();
$page->goto('https://v2.gcchmc.org/medical-status-search/');
var_dump(getToken($page->content()));
$browser->close();
Now you probably don't need the csrfmiddlewaretoken when running the script like this, but you can take it further from here if you chose to use this feature.

How to use Actions class in Selenium WebDriver/facebook WebDriver using phpunit?

I am using facebook php webdriver and I want to use Actions class to mouse hover on an element, trying different ways to do so, but not working. please help me where I am going wrong!
Here is the code-
{
$this->webDriver->get($this->url);
$id = $this->webDriver->findElement(WebDriverBy::id("email"));
$id->sendKeys("email");
$pass = $this->webDriver->findElement(WebDriverBy::id("password"));
$pass->sendKeys("passwd");
$login = $this->webDriver->findElement(WebDriverBy::xpath("//input[#value='Login']"));
$login->click();
$this->assertContains('dashboard/site',$this->webDriver->getCurrentURL());
$this->webDriver->findElement(WebDriverBy::xpath("html/body/nav[2]/div/ul/li[1]/a"))->click();
$this->webDriver->findElement(WebDriverBy::xpath("//a[contains(text(),'Care Pathways')]"))->click();
$this->webDriver->findElement(WebDriverBy::xpath("//input[#type='search']"))->sendKeys("QA Harness: Test1");
$element = $this->webDriver->findElement(WebDriverBy::xpath(".//*[#id='package-list']/tbody/tr/td[1]/a/i"));
$this->webDriver->moveToElement(WebDriverElement:: $element->isDisplayed())->perform();
//$this->webDriver->action(WebDriverActions:: )->moveToElement($element)->perform();
//$this->webDriver->getMouse()->mouseMove($element->getCoordinates());
// $this->webDriver->getMouse()->click();
//$this->webDriver->findElement(WebDriverBy::xpath("html/body/div[1]/div/div[1]/a[3]"))->click();
}
}
Use this
$action = new WebDriverActions($this->driver);
$action->moveToElement($element_you_want)->perform();
use(this for remote webdrivers):
$action = $this->driver->action();
$action->moveToElement($element_you_want)->perform();

Selenium-webdriver dropdownlist not work php

I want to choose one first drop-down list, then the second, but I can not even cope with the first
Please explain why I get the error
<?php
require_once "phpwebdriver/WebDriver.php";
$webdriver = new WebDriver("localhost", "4444");
$webdriver->connect("firefox");
$webdriver->get("http://namami.org/PKstate.aspx");
//$element = $webdriver->findElement('css selector', 'select[id="DdlState"] option[value="Assam"]');
$element = $webdriver->findElement("css selector", 'select[id="DdlState"] option[value="Assam"]')->click();
//$element=$webdriver->findElement(By.cssSelector("#DdlState")).click();
//$element->‌​click();
$webdriver->close();
?>
Fatal error: Call to undefined method WebDriver::findElement() in C:\xampp\htdocs\temp\1.php on line 7
I could not find an answer. Use XAMPP.
I would be grateful for your help.
Regards Anton
You most likely need to delay the click.
The item that you're attempting to grab with Selenium is generated by Javascript, and I think what's happening is that the Selenium is trying to find something that isn't quite rendered yet.
Try calling
sleep(1); to delay the Selenium action and see if that works:
<?php
require_once "phpwebdriver/WebDriver.php";
$webdriver = new WebDriver("localhost", "4444");
$webdriver->connect("firefox");
$webdriver->get("http://namami.org/PKstate.aspx");
sleep(1); // allow the javascript some time to load
//$element = $webdriver->findElement('css selector', 'select[id="DdlState"] option[value="Assam"]');
//$element = $webdriver->findElement("css selector", 'select[id="DdlState"] option[value="Assam"]')->click();
////$element=$webdriver->findElement(By.cssSelector("#DdlState")).click();
////$element->‌​click();
//$webdriver->close();
//?>
<?php
require_once "phpwebdriver/WebDriver.php";
$webdriver = new WebDriver("localhost", "4444");
$webdriver->connect("firefox");
$webdriver->get("http://namami.org/PKstate.aspx");
sleep(1); // allow the javascript some time to load
$element = $webdriver->findElement('id', 'DdlState');
$select = new Select($element);
$select->selectByValue("Assam");
//?>

Selenium PHP Chrome Webdriver Issue

I'm trying to get started using Selenium with Chrome, i've had no previous trouble dealing with Selenium+Firefox, but i cannot seem to launch a Chrome browser now - every time I try, a Firefox browser appears instead.
Here is my setup:
$web_driver = new ChromeDriver("C:\chromedriver\chromedriver.exe");
$session = $web_driver->session('chrome');
I realise the first line is likely not to be correct. But i cannot think of how else to initiate Chrome.
Note: I have already downloaded the chrome web driver.
Here are the sources i used:
http://edvanbeinum.com/using-selenium-2-phpunit-to-automate-browser-testing
https://code.google.com/p/selenium/wiki/ChromeDriver
Many thanks.
Try using
$session = $web_driver->session('googlechrome');
instead of
$session = $web_driver->session('chrome');
You might want to take a look here and here.
$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::htmlUnitWithJS();
{
// For Chrome
$options = new ChromeOptions();
$prefs = array('download.default_directory' => 'c:/temp');
$options->setExperimentalOption('prefs', $prefs);
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
}
$driver = RemoteWebDriver::create($host, $capabilities, 5000);

Using PHP AMQPConnection inside a thread

I have a PHP program where I connect to a Rabbit MQ server and retrieve messages. I have put this functionality inside a function:
function get_messages()
{
$connection = new AMQPConnection();
$connection->setLogin($rabbit_username);
$connection->setPassword($rabbit_passwd);
$connection->setHost($rabbit_host);
while (!$connection->connect())
{
echo "## Trying to connect to Rabbit MQ...\n";
sleep(1);
}
$amqpchn = new AMQPChannel($connection);
$mq = new AMQPQueue($amqpchn);
$mq->setName("myqueue");
$mq->setFlags(AMQP_DURABLE|AMQP_PASSIVE);
$mq->declare(); // must declare then bind
$mq->bind("my.exchange","my.routing");
// do stuff
}
This works fine. However when I try to run the function get_messages() from inside a thread (just one thread), the code gets stuck at $connection->connect(). It cannot connect to the Rabbit server.
Any ideas why this happens?
Thanks in advance

Categories