2012-06-28 34 views
0

我正在玩Behat和Selenium服務器。 我試圖根據文檔中的位置來定位特定的標籤。Selenium:該表達式不是合法表達式/ Xpath

我使用AquaPath,一個用XPath玩的GUI,並且工作正常。 我的問題是,當我在Behat/Selenium中嘗試相同的表達式時,我有一個「該表達式不是合法表達式」錯誤消息。

這裏是我的Xpath:

(//label)[5]/@for 

我在尋找具有這種屬性的第五種標記元素「的」屬性。 似乎很容易,但不工作,我真的不知道爲什麼...

下面是完整的日誌(注意// HTML自動添加):

The given selector //html//(label)[1] is either invalid or does not result in a WebElement. The following error occurred: 
[InvalidSelectorError] Unable to locate an element with the xpath expression //html//(label)[1] because of the following error: 
[Exception... "The expression is not a legal expression." code: "51" nsresult: "0x805b0033 (NS_ERROR_DOM_INVALID_EXPRESSION_ERR)" location: "file:///var/folders/66/bv_nvdt97f33pm2qn78v_8qc0000gn/T/anonymous5419845357799771264webdriver-profile/extensions/[email protected]/components/driver_component.js Line: 2621"] 
Command duration or timeout: 6 milliseconds 
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html 
Build info: version: '2.23.1', revision: '17143', time: '2012-06-08 18:59:04' 
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.7.4', java.version: '1.6.0_33' 
Driver info: driver.version: EventFiringWebDriver 
Session ID: c3bdf2e6-5042-ac4a-95c5-4735f4318899 

任何想法?

下面是HTML(標籤並不總是在同一深度):

<!DOCTYPE html> 
<html> 
    <head></head> 
    <body> 
    <form action="" method="get" accept-charset="utf-8"> 
     <label for="label1"></label> 
     <label for="label2"></label> 
     <div> 
     <label for="label3"></label>   
     </div> 
     <fieldset> 
     <div> 
      <label for="label4"></label> 
     </div>   
     </fieldset> 
     <label for="label5"></label> 
     <label for="label6"></label> 
     <label for="label7"></label> 
     <label for="label8"></label> 
     <label for="label9"></label> 
     <p><input type="submit" value="Continue &rarr;"></p> 
    </form> 
    </body> 
</html> 
+0

你能提供你正在嘗試匹配的HTML嗎? –

+0

編輯我的帖子時完成。謝謝。 – tvial

回答

0

我的看法是, 「找到()」 方法, 「XPath的」 選擇無法獲得一個屬性,只有一個元素。獲取屬性必須通過「getAttribute()」完成。

0

嘗試(//fieldset/label)[5]/@for代替(//fieldset//label)[5]/@for

+0

選擇器給出:$ this-> getSession() - > getPage() - > find('xpath',「(//fieldset//label)[".$labelHash['position']."]/@for 「); – tvial