如何使用Selenium web driver点击网页table列的超链接以及如何存储

How to Click the hyper Links in the table columns of webpage using Selenium web driver and how to store it

这是我的 HTML 脚本:

  1. 第10行的link元素如何捕获? HTML image

       <table id="dataTableParticipantSearchResults" class="display" width="100%" cellspacing="0" cellpadding="0" border="0">
       <thead>
            <tr align="left">
                <th class="ui-state-default" width="20%" style="width: 154px;">      
                  <div class="DataTables_sort_wrapper"></div>
                </th>
                <th class="ui-state-default" width="20%" style="width: 96px;"></th>
                <th class="ui-state-default" width="15%" style="width: 69px;"></th>
                <th class="ui-state-default" width="10%" style="width: 44px;"></th>
                <th class="ui-state-default" width="20%" style="width: 156px;"></th>
                <th class="ui-state-default" width="15%" style="width: 68px;"></th> 
            </tr>
        </thead>
       <tbody>
       <tr class="odd" align="left">
            <td><a href="LINK"></a></td>
       </tr>
    
       
    

2.How 来存储该值,以便我可以调用相同的值来访问 link 元素?

您总是可以通过首先识别根 table id 来找到这些类型的 hyperlinks - 在这种情况下它将是这样的:

WebElement table = driver.findelement(by.id("dataTableParticipantSearchResults"));

现在您可以使用 Xpath 或 Css 选择器来 select link

试试这个:

table.findelement(by.cssSelector("a[href='LINK']")).click();

或尝试

table.findelement(by.linktext("LINK")).click();

或尝试

table.findelement(by.xpath(".//*[@id='dataTableParticipantSearchResults']/tbody/tr/td[0]")).click();

让我知道这些是否有效....干杯!

您可以将值存储在 WebElement 中并调用任意次数。

WeElement link = table.findelement(by.xpath(".//*[@id='dataTableParticipantSearchResults']/tbody/tr/td[9]")) ;

现在调用它任意次数:

link.click();--------第1次

link.click();-------- 第二次

等等.....干杯!