In php-webdriver-bindings for selenium2 not able to store Xpath count

386 views
Skip to first unread message

Neil Anderssion

unread,
Feb 26, 2013, 7:26:53 AM2/26/13
to seleniu...@googlegroups.com

I was writing a code for Google search help functionality and want to print the related topic which will appear .

that is when we type any word in the Google search area some related topics appear under the search area , i was trying to automate that functionality using php-webdriver-bindings By Lukasz Kolczynski and trying to print those topics as out put

using Selenium Ide i can store the Xpath count in a variable and using loop i can easily print those but in selenium 2 using  php-webdriver-bindings code i am not able to print .

the code i have written so far 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 google code" ));
    $element1=$this->webdriver->findElementBy(LocatorStrategy::xpath,"//*[@id='gsr']/table/tbody/tr/td[2]/table/tbody/tr[*]/td']");

    echo $element1;

    }
}
?>



when i am trying to print element1 error is showing.

can any one please help me how i can proceed.

David

unread,
Feb 26, 2013, 6:39:48 PM2/26/13
to seleniu...@googlegroups.com
I haven't worked with the PHP binding in a while so syntax may be incorrect, but you'd want something like this:

$elements = $this->webdriver->findElementsBy(LocatorStrategy::xpath,"//*[@id='gsr']/table/tbody/tr/td[2]/table/tbody/tr[*]/td']");

echo count($elements);

Basically, WebDriver doesn't have a getXPathCount() equivalent of Selenium RC. So if your XPath locator (or other locator, like CSS, or by ID/name, etc.) returns multiple elements, you have to use the findElements method for whatever language binding you're using (not findElement).

And that will return a collection/list/array of elements which could be 0 or 1 as well that match your locator. So to figure out the count, you'd just check the size of the array in PHP.

Neil Anderssion

unread,
Feb 27, 2013, 3:26:10 AM2/27/13
to seleniu...@googlegroups.com
David thank you for your reply and suggestion .

 I changed my code and used  findElements  function  , and as per the code structure it is suppose to return  an array .  so i tried to  print the size of the array but   still error is showing i have attache the screenshot of the error , can u please check it ,  and please suggest what i have done wrong in the code 

this is the modified code


public function testSearch()
  {
//var_dump(class_exists('Testing_Selenium')); exit;

$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);
error.png

David

unread,
Feb 27, 2013, 9:18:08 PM2/27/13
to seleniu...@googlegroups.com
Sorry, I don't know what else could be the issue. Two suggestions:

* try to check if the array/result is null before checking the count, it might be failing because it's null for whatever reason.
* check with the PHP WebDriver binding developers (file a bug as needed) regarding the issue. This seems it might be specific to the PHP driver. But as a safe check, if you could, try your code in another binding to see if it is indeed a PHP driver issue or not. If not, it would fail in the other languages as well. Also check that your XPath actually will return a match result (1 or more) rather than none.

Neil Anderssion

unread,
Feb 28, 2013, 1:04:10 AM2/28/13
to seleniu...@googlegroups.com
David one important thing i also notice during debugging is

using firepath i take the xpath of the google logo  of the search result page and try to do the following thing

1 first i will go to google 
2 search with some keyword
3 at the search result page i will again click on the google logo and get back to the google search home page

for the following code it is working upto the submit process but after that clicking the logo is not working

 public function testSearch()
  {
$this->webdriver->get("http://google.com");      
    $element=$this->webdriver->findElementBy(LocatorStrategy::name,"q");
$element->sendKeys(array("selenium" ) );
  $element->submit();
$result=$this->webdriver->findElementsBy(LocatorStrategy::xpath,"//*[@id='gbq1']/a/div");
}



but when i modified the code a bit  that is  i remove the sumit command and  then clicking the  logo is working that is the following  code 

 public function testSearch()
  {
$this->webdriver->get("http://google.com");      
    $element=$this->webdriver->findElementBy(LocatorStrategy::name,"q");
$element->sendKeys(array("selenium" ) );
$result=$this->webdriver->findElementsBy(LocatorStrategy::xpath,"//*[@id='gbq1']/a/div");
}


what i am feeling that  i need to control the test speed , can u please tell be how can i controll the speed. in the binding there is a function 
setspeed( $speed) i have also tried this  but not sure what to pass in the parameter like weather i will pass "slow" "fast" something or (1000 or 2000) ms
in their documentation noting is given .


regarding the xpath which i posted earlier is correct ,because it is working fien in IDE  , but in webdriver it is returning error , i am not ableto find the reason till now.



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/4hpQVznZ4YIJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

David

unread,
Feb 28, 2013, 1:41:20 PM2/28/13
to seleniu...@googlegroups.com
Good finding Neil. I'm guessing that submit is the wrong command to use because that might make Selenium think a new page load/refresh will occur on submit but Google's pages are AJAX, so the page doesn't actually load a new one but parts of  the page are reloaded via AJAX.

So you can just omit the submit step or replace with click() command against the button element next to the text field. I also find it strange you do a submit() against the text field element too. Proper test flow / protocol has it that you submit (or click) against a submit button (or the whole form element). I don't recall if Selenium has a "submit" command, typically you just "click" the submit button to do the submit.

David

unread,
Feb 28, 2013, 1:47:50 PM2/28/13
to seleniu...@googlegroups.com
Also, for setSpeed, I don't recall either, you'd have to look at the source code to figure it out. In my fork of the bindings, I made it use a time value in seconds in my WebDriverBackedSelenium class:


As a workaround, you could also manually add sleeps in between your commands. The mentioned line of code above was a function that is implicitly called when each WebDriverBackedSelenium command is executed to add a delay (minimum of 1 second up to specified delay speed).

Neil Anderssion

unread,
Mar 1, 2013, 10:14:55 AM3/1/13
to seleniu...@googlegroups.com
Thanks David for your suggestion  , at last i am able to fix the problem . what i was suspecting ,  is the main reason behind the problem.

the problem was happening due to the speed , and to handle this i use the  " sleep () " function , and now it returning the correct result.

regarding xpath , what i found that , approach to read a xpath location in IDE  is different than webdriver ,


i was trying to find x path count using this : //*[@id='gsr']/table/tbody/tr/td[2]/table/tbody/tr[*]/td ,  as it was running correctly in IDE

but webdriver was not able to handle this xpath , 

But when i used this //td[@class='gssb_a gbqfsf']  as xpath  , the code run properly.

i dont know the reason why  webdriver was not able to handle the xpath , if u know then please share  





The code which  run successfully 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();
        
   

}

  
  
}
?>





To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/F1dqV91OYAoJ.

David

unread,
Mar 2, 2013, 1:41:37 PM3/2/13
to seleniu...@googlegroups.com
I'm not sure if Selenium IDE uses the same engine as WebDriver (even though I think it can export tests to WebDriver code format), IDE was built with Selenium RC in mind.

WebDriver / Selenium 2 had some major changes behind the scenes, one being XPath engine used. With WebDriver, it uses the browser's native XPath engine & only falls back to what was used in RC if the browser doesn't have support for it. In RC, I think all/most browsers used the same XPath engine. And assuming IDE is still based on RC, that may explain why the XPath works there but not with WebDriver.

The original XPath you had doesn't seem too bad, I think the part that killed it in the browser might be "tr[*]", as most common XPaths don't use something like that. Your second XPath is definitely cleaner and should run in all browsers with WebDriver. Stick to building your own XPaths, using the value returned from IDE as a starting reference, you'll find custom XPaths may work better and are easier to maintain long term.

Also, some people suggest moving to or trying Selenium Builder, the new replacement for IDE.
Reply all
Reply to author
Forward
0 new messages