Selenium Web Driver:php-webdriver-bindings的findElementsBy()函数返回错误

Joh*_*ohn 0 php selenium phpunit selenium-webdriver

我正在编写代码自动生成的下拉,如谷歌搜索帮助,并尝试打印自动生成的下拉值作为输出.

在selenium WebDriver中捕获多个与xpath定位符匹配的元素,我们必须使用findElementsBy()函数,

我写的代码如下

<?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);

    }
}
?>
Run Code Online (Sandbox Code Playgroud)

根据绑定,findElementsBy()函数将假设返回一个数组.所以当我试图计算数组长度时,错误正在返回.

错误:尝试获取非对象的属性.

任何人都可以帮助我如何进行.

Joh*_*ohn 6

在最后,我能够自己找到问题的解决方案.

我的主要座右铭是打印自动生成的下拉值

主要问题是测试的运行速度.由于测试速度很快,因此"findElementsBy"功能无法正常工作.

所以我在该功能之前使用了sleep命令,以便它可以正常工作.

下面给出了适合我的代码

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



    }



}
?>
Run Code Online (Sandbox Code Playgroud)