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 Stepは):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 の例で、[Try it yourself] ボタンをクリックしようとしています。

             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 に答える