5

我正在使用以下功能:

    /**
    * 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 步骤是):当我单击具有 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 很满意,但是 behat 给了我错误:

无法评估 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]"

我究竟做错了什么?

这是 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 显示正确的按钮...

4

1 回答 1

6

我有同样的问题。看来您需要省略/html/body. 为了我

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

工作正常。

于 2013-08-22T11:17:14.847 回答