Selenium-webdriver dropdownlist not work php - 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");
//?>

Related

Navigating forms and grabbing DOM-Elements from a website using Guzzle/Goutte (PHP, Debugging)

I'm fairly new to PHP and playing around with Goutte/Guzzle to grab some basic information from a website after filling out a few forms.
However I have problems finding issues (there might be a ton of them) since I could not find a way to display or console log any of the results or problems. The script finishes with Code 0 but does not return anything. A tip on how to print out what is currently stored in $client would already go a long way.
Here is the whole code I'm trying to run with a bunch of comments for clarification. I'm sorry for using such a large block, but any of this could have issues.
<?php
use Goutte\Client;
use GuzzleHttp\Client as GuzzleClient;
class grabPlate
{
// WKZ
public function checkPlate
{
$goutteClient = new Client();
$guzzleClient = new GuzzleClient(array(
'cookies' => true,
'timeout' => 60,
));
$goutteClient->setClient($guzzleClient);
$crawler = $goutteClient->request('GET', 'https://kfz-portal.berlin.de/kfzonline.public/start.html?oe=00.00.11.000000');
//Click the first "Start" in the top left
$link = $crawler
->filter('a:contains("Start")')
->eq(0)
->link()
;
$crawler = $client->click($link);
//Check checkbox, fill in name and press the button
$buttonCrawlerNode = $crawler->selectButton('Weiter');
$form = $buttonCrawlerNode->form();
$form['gwt-uid-1']->tick();
$form['select2-hidden-accessible']->select('Herr');
$form['gwt-uid-4'] = 'John';
$form['gwt-uid-5'] = 'Doe';
$client->submit($form);
//Fill some Data into the forms and search
$buttonCrawlerNode = $crawler->selectButton('Button-3616');
$form = $buttonCrawlerNode->form();
$form['.kfzonline-KennzeichenGrossEb'] = 'AB';
$form['.kfzonline-KennzeichenGrossEn'] = '123';
$client->submit($form);
//Extract collection
$info = $crawler->extract('.collection');
//return 1 if something is inside collection, 0 if it's empty
if($info == NULL) {
return 1;
} else {
return 0;
}
}
}
?>
As I said just running the script in PHPStorm returns the status 0. However when plugging it into an API and accessing it, I get a server timeout response.
You should either use a Debugging. Install xDebug to do so in PHP. This is also easy to integrate into Phpstorm.
Alternatively use var_dump() for printing out debug information about variables of any type to console.
You can display the requested page inside your php page, you have to add this snippet :
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://www.facebook.com/');
echo $res->getBody();

PHP Facebook Webdriver Click issues

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();
}

show login form in a page in joomla 1.5 programmatically

Though depreciated. still, I want to achieve this functionality and please don't advise me to upgrade to newer version of Joomla. I have tried to write this piece of code in required place in /tmp/default.php file of a particular component. Please advise me am I going right. I have still not got the answer. Thanks.
jimport('joomla.form.form');
JModel::addIncludePath(JPATH_SITE.DS.'components'.DS.'com_user'.DS.'models');
$model = JModel::getInstance('User', 'UserModel');
echo "something is not right. Please login.";
var_dump($model);
$form = $model->getLoginForm();
To include any module any where this code will be handy.
<?php
$doc = JFactory::getDocument();
$module = JModuleHelper::getModule('mod_login');
//get and update params
$params = new JRegistry;
$params->loadString($module->params);
//$params->set('param','value');
//render module
$renderer = $doc->loadRenderer('module');
$content = $renderer->render($module, array('params'=> $params));
print $content;
?>

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

Symfony 1.4 not loading sfTestFunctional failing with class not found

I've done my functional tests and now I want to run them. However, every time I run them I get sfTestFunctional class not found.
As far as I can tell the functional.php bootstrap is not autoloading the classes from the framework. Any reason why this could be?
This is my functional bootstrap
// guess current application
if (!isset($app))
{
$traces = debug_backtrace();
$caller = $traces[0];
$dirPieces = explode(DIRECTORY_SEPARATOR, dirname($caller['file']));
$app = array_pop($dirPieces);
}
require_once dirname(__FILE__).'/../../config/ProjectConfiguration.class.php';
$configuration = ProjectConfiguration::getApplicationConfiguration($app, 'test', isset($debug) ? $debug : true);
sfContext::createInstance($configuration);
// remove all cache
sfToolkit::clearDirectory(sfConfig::get('sf_app_cache_dir'));
$doctrine = new sfDoctrineDropDbTask($configuration->getEventDispatcher(), new sfAnsiColorFormatter());
$doctrine->run(array(), array("--no-confirmation","--env=test"));
$doctrine = new sfDoctrineBuildDbTask($configuration->getEventDispatcher(), new sfAnsiColorFormatter());
$doctrine->run(array(), array("--env=test"));
$doctrine = new sfDoctrineInsertSqlTask($configuration->getEventDispatcher(), new sfAnsiColorFormatter());
$doctrine->run(array(), array("--env=test"));
This is what is in my the functional tests
include(dirname(__FILE__).'/../../bootstrap/functional.php');
$browser = sfTestFunctional(new sfBrowser());
Doctrine_Core::loadData(sfConfig::get('sf_test_dir').'/fixtures/fixtures_initial.yml');
Ok. So after banging my head against the wall, I found a solution.
For some reason within the test environment custom filters are not autoloaded. The solution is to add require_once for all the custom filters to the ProjectConfiguration file. Here is the example of what I did:
if(sfConfig::get('sf_environment') == 'test' && sfConfig::get('sf_app') == 'frontend')
{
require_once sfConfig::get('sf_app_lib_dir').'/myFilter.class.php';
require_once sfConfig::get('sf_app_lib_dir').'/myotherFilter.class.php';
require_once sfConfig::get('sf_app_lib_dir').'/lovefiltersFilter.php';
require_once sfConfig::get('sf_app_lib_dir').'/eventsManagement.class.php';
require_once sfConfig::get('sf_test_dir').'/ProdPadTestFunctional.class.php';
}
I also had to add my custom testFuntional class as well. This might be more elegantly done using the autoload.yml file.
I spot the problem:
$browser = sfTestFunctional(new sfBrowser());
You should write:
$browser = new sfTestFunctional(new sfBrowser());

Categories