使用selenium查找动态文本

时间:2013-12-17 05:01:57

标签: java selenium-webdriver

我有一个动态地在跨度中放置文本的网页。它类似于:

<table id=table_1>
  <td>
    <span> $available/$total <span><label id=label_1>Servers available</label>
  </td>
</table>

我唯一知道的是网页上会显示“x / y Servers available”或“No Servers Available”。请告诉我如何才能获得此xy

<html>
  <body>
    <div class="refresh_div" >
      <table id="Server_status_table" >
      <!-- -----------For Server Status box header ---------------->
        <tr>                            
          <% 
            int num=Integer.parseInt(request.getAttribute("availWorkerCount").toString());
            int tot=Integer.parseInt(request.getAttribute("totalWorkerCount").toString());
            int avg = 0;
            if(tot!=0 && num>0){
              avg=num*100/tot;
            }
            pageContext.setAttribute("total", tot);
          %>

          <c:choose>
            <c:when test="${ total eq 0 }">
              <!-- -----------For showing how many servers are available now---------------->
              <td> 
                <label id="Avaibility_l" style="font-size: 120%;">Availability</label>
              </td>
              <td>
                <span>No server available</span>
              </td>
            </c:when>
            <c:otherwise>

              <!-- -----------For showing how many servers are available now---------------->
              <td> 
                <label id="Avaibility_l" style="font-size: 120%;">Availability</label>
                <br/> &nbsp 
              </td>
              <td >
                <!-- -----------For showing the availability bar---------------->

                <div id="progress" class="progress">
                  <div id="bar" class="bar" style="width:<%=avg %>%;"></div>
                </div>

                <!-- -----------For showing the availability progress detail message---------------->
                <span>
                  ${availWorkerCount} / ${totalWorkerCount}<label id="Progress_message_l"> occupied</label>
                </span>
              </td>
            </c:otherwise>
          </c:choose>

        </tr>
        <tr>
          <!-- -----------For showing the approximate Queue Wait time ---------------->
          <td>
            <label id="Queue_wait_time_l" style="font-size: 120%;">Queue waiting time</label>
          </td>

          <!-- -----------For showing the approximate wait time detail message---------------->
          <td >
            <label id="Wait_time _message_1stpart_l" >Approximately</label>
            ${queueWaitingTime}
          </td>

        </tr>
      </table>
    </div>
  </body>
</html>

我需要${availWorkerCount}${totalWorkerCount}${queueWaitingTime}

2 个答案:

答案 0 :(得分:0)

使用WebDriver,您可以在完成加载后从页面中选择文本。因此,获得这种“动态”文本不会成为问题。您的示例仅在服务器端是动态的,在客户端,这些变量中的文本将是静态内容,并且可以像从任何其他文本一样轻松地从页面中抓取。

我建议您将id属性添加到您要查找的<span>,然后使用Selenium获取其内容非常简单。

例如:

<span id="myStuff">foobar</span>

用以下方式获取:

String contentsOfMySpan = driver.findElement(By.id("myStuff")).getText();

*编辑*

由于您无法更改原始代码,因此执行此操作很难:

By qwt = By.xpath("//label[@id = 'Wait_time _message_1stpart_l']/parent::td/text()[2]");
By wc = By.xpath("//label[@id = 'Progress_message_l']/parent::span/text()[1]");

String queueWaitingTime = driver.findElement(qwt).getText();
String[] workerCounts = driver.findElement(wc).getText().split(" / ");
String availWorkerCount = workerCounts[0];
String totalWorkerCount = workerCounts[1];

答案 1 :(得分:0)

我有个主意。请按照以下步骤对您有所帮助,这个答案来自HTML预期。

我不确定你的意思
I need ${availWorkerCount} , ${totalWorkerCount} and ${queueWaitingTime}

步骤后的完整示例代码。简而言之,我没有运行此代码。

1.按文字搜索元素标签

label[contains(.,'Servers available')]

您可以添加ID检查,如

label[contains(@id,'label_1') and contains(.,'Servers available')]

2.获取标签,然后获得并行元素跨度。 Span应该具有值

代码:

List<WebElement> elements = driver.findElements(By.xpath("label[contains(@id,'label_1') and contains(.,'Servers available')]");
for(// now loop over the elements)
{
  WebElement elem = elements.get(0);
  List<WebElement> spanelems = elem.findElements(By.xpath("../span"));
  spanelems.get(0).getText() // this should give you the data under the span
}