2016-07-14 44 views
0

我有一個表的佈局是這樣的:如何點擊頁面上的重複按鈕?

<tbody> 
    <tr> 
     <td> 1 </td> 
     <td> <button> Click me </button> 
    </tr> 
    <tr> 
     <td> 2 </td> 
     <td> <button> Click me </button> 
    </tr> 
    .... 
    </tbody> 

我只知道第一個TD(1,2等)的值。無論如何,只有在知道第一個td時才點擊相應的第二個td(按鈕)?例如,我可以動態獲取2,並知道點擊第二個「點擊我」按鈕而不是第一個?

回答

0

您需要一個XPath:

twoClickMeButton = driver.find_element(:xpath,"//td[text()='2']/../td/button") 
1

是有辦法:

基本上,你要點擊buttons兄弟姐妹你已經知道的td元素。

請嘗試使用定位通過XPath的兄弟元素來定位這些按鈕:

//td[contains(text(),'1')]/following-sibling::* //this reads, first locate an element of td type whose text() attribute contains '1', then find its immediate sibling. 

//td[contains(text(),'2')]/following-sibling::* //this reads, first locate an element of td type whose text() attribute contains '2', then find its immediate sibling.