2013-02-12 45 views
8

收集WebElements列表後,是否可以使用相對xpath?如果是的話,最好的辦法是什麼?WebElements列表中的相對Xpath

這是我有:

List <WebElement> rows = driver.findElements(By.xpath(rowsXpath)); 
int j = 1; 
boolean foundCollector = false; 
for(WebElement e : rows) { 
    String c = e.findElement(By.xpath("(//tbody)[" + j + "]//u[text()=\"" + collector + "\"])")).getText(); 
    if(c.equals(collector)) { 
     foundCollector = true; 
     break; 
    } 
    ++j; 
} 

列表的行包含了多個列各行,我試圖找到其中包含在列的特定名稱的行。在我的for循環中,我試圖使用相對xpath來查找元素,這甚至可能嗎?或者我需要提供整個xpath我用來找到具有額外的xpath單行div的行?

我不能保證列的順序,這就是爲什麼我必須這樣做。

這是我用來獲取行的XPath:

String rowsXpath = "//div[@id=\"QosDashpardPanelBottom\"]//div[@id=\"CollectorQoSPerformanceMetricsgrid\"]//div[contains(@class, \"x-grid3-row x-unselectable-single\")]"; 

這裏的HTML:

<body class=" ext-gecko ext-windows" onload="processLoaders();"> 
<script type="text/javascript"> 
<div id="topNavDiv" style="height: 90px"> 
<div id="bodyContentOuter" style="width: 1280px; height: 587px;"> 
    <div id="bodyContentInner"> 
     <script src="RLT/RLT.nocache.js" language="javascript" type="text/javascript"> 
     <script defer="defer"> 
     <script language="javascript" type="text/javascript"> 
     <div id="rtcpMain" class=" x-component x-border-layout-ct" style="width: 1280px; height: 587px;"> 
      <div id="rtcpMainWest" class=" x-panel x-component x-border-panel" style="left: 5px; top: 5px; width: 225px;"> 
      <div id="x-auto-2" class=" x-tab-panel x-component x-border-panel" tabindex="0" hidefocus="true" style="left: 235px; top: 5px; width: 1040px;"> 
       <div class="x-tab-panel-header x-unselectable" style="width: 1038px;" unselectable="on"> 
       <div class="tone-rtcp-tabbed-content-panel" style="width: 1040px; height: 550px;"> 
        <div id="x-auto-10" class=" x-component" style="overflow: auto; width: 1040px; height: 550px;"> 
         <div id="QosDashboardPanel0" class=" x-panel x-component" style="width: 1040px;"> 
          <div id="x-auto-13" class=" x-small-editor x-panel-header x-component x-hide-display" role="presentation"> 
          <div id="rtcpDPBWrap" class="x-panel-bwrap" role="presentation" style="overflow: visible;"> 
           <div class="x-panel-tbar x-panel-tbar-noheader" role="presentation" style="width: 1040px;"> 
           <div id="rtcpDPBody" class="x-panel-body x-panel-body-noheader x-border-layout-ct" role="presentation" style="width: 1038px; height: 700px;"> 
            <div id="QosDashboardPanelTop" class=" x-component x-border-panel x-border-layout-ct" style="left: 0px; top: 0px; width: 1038px; height: 400px;"> 
            <div id="QosDashpardPanelBottom" class=" x-component x-border-panel x-border-layout-ct" style="left: 1px; top: 401px; width: 1036px; height: 298px;"> 
             <div id="QosDashpardPanel_pqosChartsLC" class=" x-component x-border-panel x-border-layout-ct x-hide-display" style="left: 0px; top: 0px; width: 1036px; height: 400px;"> 
             <div id="QosDashpardPanel_metricsTablesLC" class=" x-component x-border-panel x-border-layout-ct" style="left: 1px; top: 1px; width: 1034px; height: 296px;"> 
              <div id="CollectorMetrics_toneletWrapper" class=" x-panel x-component x-border-panel" style="left: 1px; top: 1px; width: 330px;"> 
               <div id="x-auto-99" class=" x-small-editor x-panel-header x-component x-hide-display" role="presentation"> 
               <div class="x-panel-bwrap" role="presentation"> 
                <div class="x-panel-body x-panel-body-noheader" role="presentation" style="width: 328px; height: 292px;"> 
                 <div id="CollectorMetrics" class=" x-panel x-component" style="width: 328px;"> 
                  <div id="x-auto-109" class=" x-small-editor x-panel-header x-component x-unselectable" role="presentation" unselectable="on"> 
                  <div class="x-panel-bwrap" role="presentation"> 
                   <div class="x-panel-body" role="presentation" style="width: 326px; height: 265px;"> 
                    <div id="CollectorQoSPerformanceMetricsgrid" class=" x-grid-panel x-component" style="position: relative; width: 326px; height: 265px;" tabindex="0" hidefocus="true" unselectable=""> 
                     <div class="x-grid3" role="presentation" style="width: 326px; height: 265px;"> 
                      <div class="x-grid3-viewport" role="presentation"> 
                       <div class="x-grid3-header" role="presentation"> 
                       <div class="x-grid3-scroller" role="presentation" style="width: 326px; height: 243px;"> 
                        <div class="x-grid3-body" role="presentation"> 
                         <div id="CollectorQoSPerformanceMetricsgrid_x-auto-633" class="x-grid3-row x-unselectable-single x-grid3-row-selected x-grid3-highlightrow " style="width:510px;"> 
                          <table class="x-grid3-row-table" cellspacing="0" cellpadding="0" border="0" style="width:510px;" role="presentation"> 
                           <tbody role="presentation"> 
                            <tr role="presentation"> 
                             <td id="x-auto-634" class="x-grid3-col x-grid3-cell x-grid3-td-name x-grid-cell-first " style="width:148px;" role="gridcell"> 
                              <div class="x-grid3-cell-inner x-grid3-col-name" unselectable="on"> 
                               <u>Lync</u> 
                              </div> 
                             </td> 
                             <td id="x-auto-635" class="x-grid3-col x-grid3-cell x-grid3-td-badCalls " style="width:53px;" role="gridcell"> 
                              <div class="x-grid3-cell-inner x-grid3-col-badCalls" unselectable="on">36</div> 
                             </td> 
                             <td id="x-auto-636" class="x-grid3-col x-grid3-cell x-grid3-td-totalCalls " style="width:58px;" role="gridcell"> 
                              <div class="x-grid3-cell-inner x-grid3-col-totalCalls" unselectable="on">120</div> 
                             </td> 
                             <td id="x-auto-637" class="x-grid3-col x-grid3-cell x-grid3-td-avgLatency " style="width:73px;" role="gridcell"> 
                              <div class="x-grid3-cell-inner x-grid3-col-avgLatency" unselectable="on">223</div> 
                             </td> 
                             <td id="x-auto-638" class="x-grid3-col x-grid3-cell x-grid3-td-avgLoss " style="width:53px;" role="gridcell"> 
                              <div class="x-grid3-cell-inner x-grid3-col-avgLoss" unselectable="on">0.80</div> 
                             </td> 
                             <td id="x-auto-639" class="x-grid3-col x-grid3-cell x-grid3-td-avgJitter " style="width:58px;" role="gridcell"> 
                              <div class="x-grid3-cell-inner x-grid3-col-avgJitter" unselectable="on">29</div> 
                             </td> 
                             <td id="x-auto-640" class="x-grid3-col x-grid3-cell x-grid3-td-avgMOS x-grid3-cell-last " style="width:53px;" role="gridcell"> 
                              <div class="x-grid3-cell-inner x-grid3-col-avgMOS" unselectable="on">3.86</div> 
                             </td> 
                            </tr> 
                           </tbody> 
                          </table> 
                         </div> 
                         <div id="CollectorQoSPerformanceMetricsgrid_x-auto-641" class="x-grid3-row x-unselectable-single " style="width:510px;"> 
                          <table class="x-grid3-row-table" cellspacing="0" cellpadding="0" border="0" style="width:510px;" role="presentation"> 
                           <tbody role="presentation"> 
                            <tr role="presentation"> 
                            <td id="x-auto-642" class="x-grid3-col x-grid3-cell x-grid3-td-name x-grid-cell-first " style="width:148px;" role="gridcell"> 
                             <div class="x-grid3-cell-inner x-grid3-col-name" unselectable="on"> 
                              <u>CUCM-Publisher</u> 
                             </div> 
                            </td> 
                            <td id="x-auto-643" class="x-grid3-col x-grid3-cell x-grid3-td-badCalls " style="width:53px;" role="gridcell"> 
                             <div class="x-grid3-cell-inner x-grid3-col-badCalls" unselectable="on">3</div> 
                            </td> 
                            <td id="x-auto-644" class="x-grid3-col x-grid3-cell x-grid3-td-totalCalls " style="width:58px;" role="gridcell"> 
                             <div class="x-grid3-cell-inner x-grid3-col-totalCalls" unselectable="on">52</div> 
                            </td> 
                            <td id="x-auto-645" class="x-grid3-col x-grid3-cell x-grid3-td-avgLatency " style="width:73px;" role="gridcell"> 
                             <div class="x-grid3-cell-inner x-grid3-col-avgLatency" unselectable="on">190</div> 
                            </td> 
                            <td id="x-auto-646" class="x-grid3-col x-grid3-cell x-grid3-td-avgLoss " style="width:53px;" role="gridcell"> 
                             <div class="x-grid3-cell-inner x-grid3-col-avgLoss" unselectable="on">0.79</div> 
                            </td> 
                            <td id="x-auto-647" class="x-grid3-col x-grid3-cell x-grid3-td-avgJitter " style="width:58px;" role="gridcell"> 
                             <div class="x-grid3-cell-inner x-grid3-col-avgJitter" unselectable="on">31</div> 
                            </td> 
                            <td id="x-auto-648" class="x-grid3-col x-grid3-cell x-grid3-td-avgMOS x-grid3-cell-last " style="width:53px;" role="gridcell"> 
                             <div class="x-grid3-cell-inner x-grid3-col-avgMOS" unselectable="on">3.98</div> 
                            </td> 
                            </tr> 
                           </tbody> 
                          </table> 
                         </div> 
                        </div>                           

回答

12

是的,這是絕對有可能的。看到這一點:

// Find the outer element. 
WebElement outer = driver.findElement(By.anything("whatever")); 

// This doesn't work, it searches the whole page for the XPath expression. 
WebElement inner = outer.findElement(By.xpath("//some/nasty/xpath")); 

// This works! It takes the outer element as root. 
WebElement inner = outer.findElement(By.xpath(".//some/nasty/xpath")); 

因此,如果我理解你的需求很好,你的XPath表達式需要看起來像這樣:

By.xpath("(.//tbody)[" + j + "]//div[text()=\"" + collector + "\"])") 

編輯(天哪,這是一個很大的HTML並且實際上只需要少量的:) :)

現在我發現了你以後的事情,我認爲你根本不需要這種方法。試試這個:

WebElement row = driver.findElement(By.xpath("id('CollectorQoSPerformanceMetricsgrid')//div[contains(@class,'x-grid3-row x-unselectable-single') and .//u[text()='" + collector + "']]")); 

只有XPath表達式:

id('CollectorQoSPerformanceMetricsgrid') 
    //div[contains(@class,'x-grid3-row x-unselectable-single') 
      and .//u[text()='" + collector + "'] ] 

拆開:

FIND ELEM WITH ID = 'CollectorQoSPerformanceMetricsgrid' 
id('CollectorQoSPerformanceMetricsgrid') 
    FIND ALL DIV ELEMS THAT CONTAIN 'x-grid3-row x-unselectable-single' IN @CLASS 
    //div[contains(@class,'x-grid3-row x-unselectable-single') 
      AND HAVE AN <U> DESCENDANT WITH TEXT EQUAL TO collector VARIABLE 
      and .//u[text()='" + collector + "'] ] 

此認定你只單列你需要用一個命令。

如果你寧願要堅持使用當前的解決方案,嘗試:

String rowsXpath = "id('CollectorQoSPerformanceMetricsgrid')//div[contains(@class, 'x-grid3-row x-unselectable-single')]"; 
List<WebElement> rows = driver.findElements(By.xpath(rowsXpath)); 
boolean foundCollector = false; 
for(WebElement row : rows) { 
    if(!row.findElements(By.xpath(".//u[text()='" + collector + "']")).isEmpty()) { 
     foundCollector = true; 
     break; 
    } 
} 
+0

我試過了但它不起作用,我沒有得到任何結果。我想我遇到了訪問單個行的問題。完整的xpath,如果我嘗試「//nasty/xpath/(.//tbody)[1]」在螢火蟲例如我沒有得到任何回報。 – 2013-02-12 19:06:46

+0

@ alexxp03我每天都使用它,所以這個構造肯定有效。你只需要拿出正確的Xpath。如果可以的話,在這裏發佈你的HTML代碼,我們會解決它! – 2013-02-12 19:11:28

+0

我會很高興,這讓我瘋狂。 html是相當大的什麼是獲得它併發布它的最佳方式? – 2013-02-12 19:23:12

1

無處我發現行(tr標籤)在此XPath那麼它是如何讓行計數。

String rowsXpath = "//div[@id=\"QosDashpardPanelBottom\"]//div[@id=\"CollectorQoSPerformanceMetricsgrid\"]//div[contains(@class, \"x-grid3-row x-unselectable-single\")]"; 

需要構建的XPath類似下面讓行數在特定表

int rowCount=driver.findElements(By.xpath("//table[@class='x-grid3-row-table']/tbody/tr")) 

或者,如果你想找到該表中的所有列具有特定名稱

結構的XPath其他像下面一樣

int columnCount=driver.findElements(By.xpath("//table[@class='x-grid3-row-table']/tbody//tr//td[@name='RequiredName']")) 
+0

我的問題在於找到整體後我可以使用相對路徑。假設你的rowCount xpath是正確的,我將如何訪問由rowCount重印的行中的單個列值? – 2013-02-13 06:24:37

相關問題