如何在表中收集特定的行id - 使用java集合的webdriver

时间:2014-10-09 08:01:54

标签: java selenium selenium-webdriver

Html代码

<table id="tblRenewalsStatus" class="bor-bot" width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr id="trVerifyPayment">
<tr id="trAssignAgent">
<tr id="trAssigned">
<tr id="trPayAgent" style="display: none;">
<tr id="trPaymentVerificationPending" style="display: none;">
<tr id="trInProgress" style="display: none;">
<tr id="trPayAgentCommission">
<tr id="trPaymentVerificationPendingCommission">
<tr id="trCompleted">
<tr id="trCancelled">
</tbody>
</table>

我想收集所有没有样式属性值的tr id,

预期产出,

&#34; trVerifyPayment&#34;&#34; trAssignAgent&#34;&#34; trAssigned&#34;,trPayAgentCommission&#34;&#34; trPaymentVerificationPendingCommission&#34;&#34; trCompleted&# 34;,&#34; trCancelled&#34;

3 个答案:

答案 0 :(得分:1)

List<WebElement> id_elements = driver.findElements(By.xpath("//table[@id='tblRenewalsStatus']//tr"));

    ArrayList<String> tr_id= new ArrayList<String>();
    for(WebElement ele : id_elements)
     {

        String style = ele.getAttribute("style");

        if(style==null||style=="")
        {
            String id=ele.getAttribute("id");
            tr_id.add(id);
        }
      }

    System.out.println(tr_id);

答案 1 :(得分:1)

WebElement RenewalTable = driver
                .findElement(By.id("tblRenewalsStatus"));
        List<WebElement> allRows = RenewalTable.findElements(By.tagName("tr"));
        ArrayList<String> enableRow = new ArrayList<String>();
        for (WebElement eachElement : allRows) {

            String style = eachElement.getAttribute("style");

            if (style.equals("")) {
                String id = eachElement.getAttribute("id");
                enableRow.add(id);
            }
        }

答案 2 :(得分:-1)

找到没有某些属性的元素的问题在这里解决了:

https://stackoverflow.com/a/18774549/2131257

并且查找所有元素 s 的方法是

driver.findElements(By.id("element_id")) 

返回元素列表而不是

driver.findElement(By.id("element_id"))

只返回一个元素。

希望它有所帮助。