2014-02-13 129 views
2

我有這樣的代碼XPath來獲取複選框內標籤

<label> 
    <input type="radio" checked="checked" value="fOejPdlZIx83HA" name="btnRad"> 
    Test1 
</label> 
<label> 
    <input type="radio" checked="checked" value="fdsaf4waff4sssd" name="btnRad"> 
    Test2 
</label> 
<label> 
    <input type="radio" checked="checked" value="fg43fasd43wsat4" name="btnRad"> 
    Test3 
</label> 

我想通過XPath來訪問取決於標籤文本的單選按鈕

我已經嘗試過多次的事情:

//input[@name='btnRad]']/following::*[contains(text(),'Test3')] 
//label[text()='Test3']/input[@name='btnRad'] 
//*[contains(text(),'Test3')] 

即使最後一個沒有任何回報,所以xpath認爲「Test3」不是標籤的文本...任何人都有一個想法如何做到這一點?

+0

我測試了firefox中最後一個xpath查詢,使用了很棒的插件FirePath,它的工作原理 – emcas88

+0

我真的不知道該怎麼告訴你......我也試過最後一個firepath,它沒有返回結果:/ – Etienne

+0

我測試了在這個相同的頁面,它的工作,非常奇怪... – emcas88

回答

4

由於您的label包含多個文本節點,因此您的表達式失敗:inputTest3之前的空字符串。你使用的方式contains意味着它只會檢查第一個文本節點,即空字符串。

兩種方式解決這個的:

  • 消除與normalize-space()空字符串:

    //*[contains(text()[normalize-space()], 'Test3')] 
    
  • 每個text()查詢:

    //*[text()[contains(.,'Test3')]] 
    

更詳細的解釋,請參閱How to search for content in XPath in multiline text using Python?

+0

禾rk完美與正常化空間,thx! – Etienne

0

這也適用於//label[contains(.,'Test3')]/input