1

次のマークアップがあります。

<div>
 <div>Question1</div>
 <div>
  <button type="button">No</button>
  <button type="button">Yes</button>
 </div>
</div>

<div>
 <div>Question2</div>
 <div>
  <button type="button">No</button>
  <button type="button">Yes</button>
 </div>
</div>

次の XPath 式は、2 つの [はい] ボタンを見つけます。

//div/div/button[text()='Yes']  

ただしYes、次のように最初の div のボタンのみが必要です。

//div/div[text()='Question1']

どうすればそれを見つけることができますか?

4

6 に答える 6

2

使用:

//div[div[. = 'Question1']]
   /div/button[@type = 'button'][. = 'Yes']

これbuttonは、文字列値が"Yes"で、type属性が文字列値"button"を持ち、(button要素) が文字列値を持つ子を持つ a の子divであるすべてを選択します。divdiv"Question1"

于 2012-04-23T13:25:59.663 に答える
1

次のように最初のターゲットを設定できます(クエリbuttonにハードコーディングしたくない場合):Question1

(//div/div/button[text() = 'Yes'])[1]

MSDNXPathの例も参照してください。

于 2012-04-23T13:10:12.460 に答える
1

あなたのスニペットでテストされ、私のために働いています:

//div/div[preceding-sibling::*[position() = 1][.="Question1"]]/button[.="Yes"]
于 2012-04-23T13:11:40.773 に答える
0

このようなものを試してください

/x:html/x:body/x:div[1]/x:div[2]/x:button[2]
于 2012-04-23T13:08:19.220 に答える
0

これでうまくいくはずです。
//div[div[text()='Question1']]/div/button[text()='Yes']
または
//div[div/text()='Question1']/div/button[text()='Yes']
これにより、text ='Yes'で、text='Question1'の子divを持つ親を持つボタンが選択されます。

また、テキストに関係なく最初のdivに関心がある場合は、次を使用します。

//div[1]/div/button[text()='Yes']
より具体的には、親を指定する必要があります。-
/div[1]/div/button[text()='Yes']ルートの場合(例のように) //parentNode/div[1]/div/button[text()='Yes']-ルートの場合parentNode

于 2012-04-23T13:08:27.677 に答える
0

//div/div[text()='Question1']/button[text()='Yes'] を試してください

于 2012-04-23T13:07:57.840 に答える