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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With