2013-02-12 55 views
8

क्या वेबपृष्ठों की सूची एकत्र करने के बाद एक सापेक्ष 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; 
} 

सूची पंक्तियों एकाधिक स्तंभों के साथ अलग-अलग पंक्तियों में शामिल है और मैं पंक्ति जो एक कॉलम में किसी विशेष नाम होता है खोजने की कोशिश कर रहा हूँ:

यह है कि मैं क्या है। मेरे लूप के भीतर मैं एक तत्व की तलाश करने के लिए एक सापेक्ष xpath का उपयोग करने की कोशिश कर रहा था, क्या यह भी संभव है? या क्या मुझे व्यक्तिगत xsath के लिए अतिरिक्त xpath के साथ पंक्तियों को खोजने के लिए उपयोग किया गया पूरा xpath प्रदान करने की आवश्यकता है?

मुझे कॉलम के आदेश की गारंटी नहीं है, इसलिए मुझे इसे इस तरह से करना है।

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

यहाँ है एचटीएमएल:

<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

हाँ, यह निश्चित रूप से संभव है

यह xpath मैं पंक्तियों प्राप्त करने के लिए प्रयोग किया जाता है। देखें:

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

संपादित (पवित्र गाय, कि HTML के एक बहुत कुछ है:

// 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 अभिव्यक्ति इस तरह देखने के लिए की जरूरत है और इसकी केवल एक छोटी राशि वास्तव में आवश्यक है :)।)

अब जब मैंने आपके पीछे क्या पकड़ा, तो मुझे लगता है कि आपको इस दृष्टिकोण की आवश्यकता नहीं है। ऐसा करें:

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

मैंने कोशिश की लेकिन यह काम नहीं करता है, मुझे कोई परिणाम नहीं मिलता है।मुझे लगता है कि मैं व्यक्तिगत पंक्तियों तक पहुंचने में एक समस्या में भाग रहा हूं। अगर मैं फायरबग पर "//nasty/xpath/(.//tbody)[1]" कोशिश करता हूं तो पूर्ण xpath मुझे कुछ भी वापस नहीं मिलता है। –

+0

@ alexxp03 मैं इसे दैनिक आधार पर उपयोग करता हूं, इसलिए निर्माण निश्चित रूप से काम करता है। आपको सिर्फ सही एक्सपैथ के साथ आना होगा। यदि आप कर सकते हैं, तो अपना HTML कोड यहां पोस्ट करें, हम इसे काम करेंगे! –

+0

मुझे खुशी होगी, यह मुझे पागल कर रहा है। एचटीएमएल काफी बड़ा है इसे पाने और इसे पोस्ट करने का सबसे अच्छा तरीका क्या है? –

1

कहीं मैं तो इस xpath में पंक्ति (टीआर टैग) पाया कि यह कैसे पंक्तियों गिनती मिल जाएगा।

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

आप विशेष तालिका में पंक्ति संख्या प्राप्त करने के लिए

int rowCount=driver.findElements(By.xpath("//table[@class='x-grid3-row-table']/tbody/tr")) 
वरना यदि आप विशेष रूप से नाम

निर्माण xpath साथ कि तालिका के सभी कॉलम लगाना चाहते हैं

नीचे की तरह xpath कुछ का निर्माण करने की जरूरत है

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

में अग्रणी बिंदु की आवश्यकता नहीं है, यह मेरा प्रश्न था कि क्या मैं पूरे खोजने के बाद सापेक्ष पथ का उपयोग कर सकता था। अपनी पंक्ति मानते हुए xpath सही है, मैं rowCount द्वारा पुन: मुद्रित पंक्ति के भीतर व्यक्तिगत कॉलम मानों का उपयोग कैसे करूं? –