2017-04-26 95 views
0

下面是我遇到問題的HTML部分。我怎樣才能點擊一個表中的按鈕?

我在試圖做的事:我目前使用硒鉻驅動程序來完成在網站上創建票證的過程。在某個頁面上,我必須選擇一個標籤才能檢查一些複選框(見下文)。但是,它們都具有相同的ID。我如何使用C#中的selenium chrome驅動程序來選擇(例如第二個TOP_TAB & TOP_BUTTON),然後單擊該元素。

<input type="hidden" name="tabsize" value="5"> 

<td id="TOP_TAB" width="10" class="leftRightBorder" style="width: 5px;"> 
    <table cellpadding="0" cellspacing="0"> 
     <tbody> 
      <tr> 
       <td id="TOP_BUTTON" style="cursor:hand;" onclick="fun_HideShowBlock('0','0');" align="center" nowrap=""><img src="images/left-cor.gif" width="5" height="18" border="0"></td> 
       <td id="TOP_BUTTON" style="cursor: pointer; font-size: 10px; background-color: rgb(181, 200, 217); border-bottom: 2px solid rgb(181, 200, 217); font-weight: normal;" 
        onclick="fun_HideShowBlock('0','0');" align="center" class="drop" nowrap="" bgcolor="#B5C8D9" width="88">Addendum 02</td> 
       <td id="TOP_BUTTON" style="cursor:hand;" onclick="fun_HideShowBlock('0','0');" align="center" nowrap=""><img src="images/right-cor.gif" width="5" height="18" border="0"></td> 
      </tr> 
     </tbody> 
    </table> 
</td> 

<td id="TOP_TAB" width="10" class="leftRightBorder" style="width: 5px;"> 
    <table cellpadding="0" cellspacing="0"> 
     <tbody> 
      <tr> 
       <td id="TOP_BUTTON" style="cursor:hand;" onclick="fun_HideShowBlock('1','1');" align="center" nowrap=""><img src="images/left-cor-light.gif" width="5" height="18" border="0"></td> 
       <td id="TOP_BUTTON" style="cursor: pointer; font-size: 10px; background-color: rgb(217, 223, 239); border-bottom: 0px solid rgb(217, 223, 239); font-weight: bold;" 
        onclick="fun_HideShowBlock('1','1');" align="center" class="drop" nowrap="" bgcolor="#B5C8D9" width="88">Addendum 01</td> 
       <td id="TOP_BUTTON" style="cursor:hand;" onclick="fun_HideShowBlock('1','1');" align="center" nowrap=""><img src="images/right-cor-light.gif" width="5" height="18" border="0"></td> 
      </tr> 
     </tbody> 
    </table> 
</td> 

<td id="TOP_TAB" width="10" class="leftRightBorder" style="width: 5px;"> 
    <table cellpadding="0" cellspacing="0"> 
     <tbody> 
      <tr> 
       <td id="TOP_BUTTON" style="cursor:hand;" onclick="fun_HideShowBlock('2','2');" align="center" nowrap=""><img src="images/left-cor.gif" width="5" height="18" border="0"></td> 
       <td id="TOP_BUTTON" style="cursor: pointer; font-size: 10px; background-color: rgb(181, 200, 217); border-bottom: 2px solid rgb(181, 200, 217); font-weight: normal;" 
        onclick="fun_HideShowBlock('2','2');" align="center" class="drop" nowrap="" bgcolor="#B5C8D9" width="88">Default Job</td> 
       <td id="TOP_BUTTON" style="cursor:hand;" onclick="fun_HideShowBlock('2','2');" align="center" nowrap=""><img src="images/right-cor.gif" width="5" height="18" border="0"></td> 
      </tr> 
     </tbody> 
    </table> 
</td> 

<td id="TOP_TAB" width="10" class="leftRightBorder" style="width: 5px;"> 
    <table cellpadding="0" cellspacing="0"> 
     <tbody> 
      <tr> 
       <td id="TOP_BUTTON" style="cursor:hand;" onclick="fun_HideShowBlock('3','3');" align="center" nowrap=""><img src="images/left-cor.gif" width="5" height="18" border="0"></td> 
       <td id="TOP_BUTTON" style="cursor: pointer; font-size: 10px; background-color: rgb(181, 200, 217); border-bottom: 2px solid rgb(181, 200, 217); font-weight: normal;" 
        onclick="fun_HideShowBlock('3','3');" align="center" class="drop" nowrap="" bgcolor="#B5C8D9" width="88">Default Job</td> 
       <td id="TOP_BUTTON" style="cursor:hand;" onclick="fun_HideShowBlock('3','3');" align="center" nowrap=""><img src="images/right-cor.gif" width="5" height="18" border="0"></td> 
      </tr> 
     </tbody> 
    </table> 
</td> 

<td id="TOP_TAB" width="10" class="leftRightBorder" style="width: 5px;"> 
    <table cellpadding="0" cellspacing="0"> 
     <tbody> 
      <tr> 
       <td id="TOP_BUTTON" style="cursor:hand;" onclick="fun_HideShowBlock('4','4');" align="center" nowrap=""><img src="images/left-cor.gif" width="5" height="18" border="0"></td> 
       <td id="TOP_BUTTON" style="cursor: pointer; font-size: 10px; background-color: rgb(181, 200, 217); border-bottom: 2px solid rgb(181, 200, 217); font-weight: normal;" 
        onclick="fun_HideShowBlock('4','4');" align="center" class="drop" nowrap="" bgcolor="#B5C8D9" width="88">Default Job</td> 
       <td id="TOP_BUTTON" style="cursor:hand;" onclick="fun_HideShowBlock('4','4');" align="center" nowrap=""><img src="images/right-cor.gif" width="5" height="18" border="0"></td> 
      </tr> 
     </tbody> 
    </table> 
</td> 

+0

我將執行XPath來選擇所有TOP_TAB元素(循環直到其需要的),然後第二個XPath查找所有的TOP_BUTTON(迭代並選擇我需要的索引) –

回答

0

如果你知道它永遠是你能做到這一點(的按鈕和同樣的想法),第二個選項卡。

driver.FindElements(By.Xpath("descendant::td[@id = 'TOP_TAB']"))[1]; 

,如果你不知道,這將是第二個,但知道你需要一個與附錄01的話,你可以這樣做:

driver.FindElement(By.Xpath("descendant::td[text() = 'Addendum 01']/ancestor::td[@id = 'TOP_TAB']")); 

第二種方式讓你的你想要的物品,然後回到你想要的標籤。

+0

這第一個工作完美。感謝你的這一點。 –

+0

隨時接受答案,如果它的工作,以便其他人會知道它是正確的:) –

相關問題