Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not evaluate XPath (Behat/Mink)

Tags:

xpath

behat

mink

I'm using the following function:

    /**
    * Click on the element with the provided xpath query
    *
    * @When /^I click on the element with xpath "([^"]*)"$/
    */
   public function iClickOnTheElementWithXPath($xpath)
   {
       $session = $this->getSession(); // get the mink session
       $element = $session->getPage()->find('xpath',$session->getSelectorsHandler()->selectorToXpath('xpath', $xpath)); // runs the actual query and returns the element

       // errors must not pass silently
       if (null === $element) {
           throw new \InvalidArgumentException(sprintf('Could not evaluate XPath: "%s"', $xpath));
       }

       // ok, let's click on it
       $element->click();

   }

in my FeatureContext.php trying to click on a button with the following XPath (the Behat Step is): When I click on the element with XPath

//html/body/div[4]/div/div/div/div[2]/div[2]/div[4]/div/div/div/div[2]/ol/li/span[4]/a[1]

Firebug is happy with this XPath, but behat is giving me the error:

Could not evaluate XPath: "//html/body/div[4]/div/div/div/div[2]/div[2]/div[4]/div/div/div/div[2]/ol/li/span[4]/a[1]"

What am I doing wrong?

here is an example of the Behat on w3schools, trying to click on the "Try it yourself" button":

             Feature: xpath try on w3schools.com/tags/att_input_src.asp 
             Scenario: click button on xpath 
              @javascript
             When I go to "/tags/att_input_src.asp" 
              And I click on the element with xpath "/html/body/div/div[3]/div[6]/div/a[1]" 
              And I wait 5 seconds
             Then I should be on "tags/tryit.asp?filename=tryhtml_input_src"

gives the same error, could not evaluate xpath, the xpath on Firebug shows the correct button...

like image 320
Ginz Its Avatar asked Aug 08 '13 16:08

Ginz Its


1 Answers

I had the same problem. It appears that you need to ommit /html/body. For me

//div[2]/div/div[2]/div/table/tbody/tr/td[3]

works fine.

like image 149
eventhorizon.pl Avatar answered Oct 13 '22 17:10

eventhorizon.pl