我正在使用以下功能:
/**
* 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();
}
在我的FeatureContext.php中试图点击一个带有以下XPath的按钮(Behat Step is):当我点击带有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对这个XPath很满意,但是这给了我错误:
无法评估XPath:“// html / body / div [4] / div / div / div / div [2] / div [2] / div [4] / div / div / div / div [2 ] /醇/ LI /跨度[4] / A [1]“
我做错了什么?
这里是w3schools上Behat的一个例子,试图点击“自己试试”按钮“:
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"
给出相同的错误,无法评估xpath,Firebug上的xpath显示正确的按钮...
答案 0 :(得分:6)
我遇到了同样的问题。您似乎需要省略/html/body
。对我来说
//div[2]/div/div[2]/div/table/tbody/tr/td[3]
工作正常。