John John - 3 months ago 31
PHP Question

Selenium Web Driver :findElementsBy() function of php-webdriver-bindings returning error

I was writing the code auto generated drop like google search help and trying to print the values of that auto generated drop down as output.

in selenium WebDriver to catch multiple elements which will match with a xpath locator we have to use findElementsBy() function,

the code i have written is given below

<?php
require_once 'www/library/phpwebdriver/WebDriver.php';
class PHPWebDriverTest extends PHPUnit_Framework_TestCase {
protected $webdriver;

protected function setUp() {
$this->webdriver = new WebDriver("localhost", 4444);
$this->webdriver->connect("firefox");
}

protected function tearDown() {
// $this->webdriver->close();
}
public function testgooglesearch() {
$this->webdriver->get("http://google.com");
$element=$this->webdriver->findElementBy(LocatorStrategy::name, "q");
$element->sendKeys(array("selenium" ) );
$result=$this->webdriver->findElementsBy(LocatorStrategy::xpath,"//*[@id=\'gsr\']/table/tbody/tr/td[2]/table/tbody/tr[*]/td/");
echo $countresult=count($result);

}
}
?>


As per the binding findElementsBy() function will suppose to return an array. so when i am trying to count the array length a error is returning .

error : Trying to get property of non -object.

can any one please help me how i can proceed.

Answer

At Last i am able to find the solution of the problem on my own .

My main motto was to print the value of the auto generated drop down

The main problem was the running speed of the test .As the speed of test was fast so " findElementsBy " function was not able to work properly.

so i used sleep command just before that function so that it can work properly .

The code which work properly for me is given below

<?php
require_once "/phpwebdriver/WebDriver.php";
class WebdriverTest extends PHPUnit_Framework_TestCase

{
  protected $webdriver;

  protected function setUp()
  {   

$this->webdriver=new WebDriver("localhost", 4444);

    $this->webdriver->connect("firefox");
  }

   protected function tearDown() {
      $this->webdriver->close();
    }

  public function testSearch()
  {
    $this->webdriver->get("http://google.com");         

       $element=$this->webdriver->findElementBy(LocatorStrategy::name,"q");
    $element->sendKeys(array("selenium" ) );

sleep(2);
    $result=$this->webdriver->findElementsBy(LocatorStrategy::xpath,"//td[@class='gssb_a gbqfsf']");
    $countresult=count($result);
    echo "Records Count = ". $countresult ."\n";
        $x=1;
    $y=0;
    echo "\n";
     while($y<$countresult)
      {
        $output=$result[$y]->getText();
    echo $output."\n";
         $x++;
         $y++;
       }

    $r=$this->webdriver->findElementBy(LocatorStrategy::xpath,"//div[@class='gbqlca']");
    $r->click();



    }



}
?>
Comments