2012-09-20 55 views
0

Scenerio:在一個網頁中有幾個表格。我想完整閱讀其中一個表格,但問題是僅顯示10行,因此我必須向下滾動。實際上原來只有10行可用,當我們滾動時其餘的字段都呈現。 爲了解決這個問題,我以爲我會模擬鍵和繼續閱讀,但問題是即使xpath不一致通過了所以不能放在一個循環太。幾種不同的單元格的Xpath是:Selenium Webdriver:完全讀取表格

html/body/div[2]/form/div[1]/div[2]/div/div[2]/div/div[2]/div/div[2]/div/div[2]/div/span/div[1]/div/div/div/div[2]/div/div[2]/table/tbody/tr[1]/td[1] 

html/body/div[2]/form/div[1]/div[2]/div/div[2]/div/div[2]/div/div[2]/div/div[2]/div/span/div[1]/div/div/div/div[2]/div/div[2]/table[2]/tbody/tr[3]/td[1] 

html/body/div[1]/form/div[1]/div[2]/div/div[2]/div/div[2]/div/div[2]/div/div[2]/div/span/div[1]/div/div/div/div[2]/div/div[2]/table[1]/tbody/tr[10]/td[1] 

以何種方式可以在所有單元格中獲取數據?

HTML來源:滾動後

<div id="pt1:ph1" class="x6v"> 
    <table class="xtb" width="0" cellspacing="0" cellpadding="0" border="0" summary=""> 
    <div class="xvp"> 
    <div> 
    <div id="pt1:pc1" class="xpb xph" _afrclmwmn="['c1','c2','c3','c4','c5','c6','c7','c8','c9','c10']" _afrac="pt1:pc1:md1" style="height: 282px;"> 
    <div id="pt1:pc1::_ahTp" style="height:auto"> 
    <div id="pt1:pc1::_ahCt"> 
    <div id="pt1:pc1:md1" class="xpj xpb" _leafcolclientids="['pt1:pc1:md1:c1','pt1:pc1:md1:c2','pt1:pc1:md1:c3','pt1:pc1:md1:c4','pt1:pc1:md1:c5','pt1:pc1:md1:c6','pt1:pc1:md1:c7','pt1:pc1:md1:c8','pt1:pc1:md1:c9','pt1:pc1:md1:c10','pt1:pc1:md1:c11','pt1:pc1:md1:c12']" _afrfilterable="true" _afrautohr="10" _afrhcc="0" _afrpcid="pt1:pc1" tabindex="0" style="height: 234px;"> 
    <div id="pt1:pc1:md1::ch" class="xz5" _afrcolcount="12" style="overflow: hidden; position: relative; width: 753px;"> 
    <div id="pt1:pc1:md1::db" class="xyy" _afrcolcount="12" style="position: relative; width: 753px; overflow: hidden; height: 170px; z-index: 1;"> 
    <table class="xyz xzr" cellspacing="0" _startrow="0" _rowcount="44" _selstate="{'0':true}" _totalwidth="1260" style="table-layout:fixed;position:relative;width:1260px;"> 
    <tbody> 
    <tr class="xzn p_AFSelected" _afrrk="0"> 
    <td class="xzk" nowrap="" style="width:100px;">12</td> 
    <td class="xzk" nowrap="" style="width:100px;">A12</td> 
    <td class="xzk" nowrap="" style="width:100px;">B12</td> 
    <td class="xzk" nowrap="" style="width:100px;">B12</td> 
    <td class="xzk" nowrap="" style="width:100px;">C12</td> 
    <td class="xzk" nowrap="" style="width:100px;">D12</td> 
    <td class="xzk" nowrap="" style="width:100px;"> </td> 
    <td class="xzk" nowrap="" style="width:100px;">K12</td> 
    <td class="xzk" nowrap="" style="width:100px;"> </td> 
    <td class="xzk" nowrap="" style="width:100px;"> </td> 
    <td class="xzk" nowrap="" style="width:100px;"> </td> 
    <td class="xzk" nowrap="" style="width:100px;">G12</td> 
    </tr> 
    <tr class="xzn" _afrrk="1"> 
    <tr class="xzn" _afrrk="2"> 
    <tr class="xzn" _afrrk="3"> 
    <tr class="xzn" _afrrk="4"> 
    <tr class="xzn" _afrrk="5"> 
    <tr class="xzn" _afrrk="6"> 
    <tr class="xzn" _afrrk="7"> 
    <tr class="xzn" _afrrk="8"> 
    <tr class="xzn" _afrrk="9"> 
    </tbody> 
    </table> 
    </div> 
    <div id="pt1:pc1:md1::sm" class="xzu" style="position:absolute;display:none"></div> 
    <div id="pt1:pc1:md1::ri" class="xz0" style="position:absolute;display:none;overflow:hidden"></div> 
    <div id="pt1:pc1:md1::dataW" style="display:none"></div> 
    <div id="pt1:pc1:md1::scroller" tabindex="-1" style="position: absolute; overflow: auto; z-index: 0; width: 770px; top: 46px; height: 187px; right: 0px;"> 
    </div> 
    </div> 
    <div id="pt1:pc1::_ahBt" style="height:auto"> 
    <div id="pt1:pc1:_clmCxt" style="display:none"> 
    <div id="pt1:pc1:_PCPop" style="display:none"> 
    <div id="pt1:pc1::_dchDlgC" style="display:none"> 
    </div> 
    </div> 
    </div> 
    </div> 

HTML。

<div id="pt1:ph1" class="x6v"> 
    <table class="xtb" width="0" cellspacing="0" cellpadding="0" border="0" summary=""> 
    <div class="xvp"> 
    <div> 
    <div id="pt1:pc1" class="xpb xph" _afrclmwmn="['c1','c2','c3','c4','c5','c6','c7','c8','c9','c10']" _afrac="pt1:pc1:md1" style="height: 282px;"> 
    <div id="pt1:pc1::_ahTp" style="height:auto"> 
    <div id="pt1:pc1::_ahCt"> 
    <div id="pt1:pc1:md1" class="xpj xpb" _leafcolclientids="['pt1:pc1:md1:c1','pt1:pc1:md1:c2','pt1:pc1:md1:c3','pt1:pc1:md1:c4','pt1:pc1:md1:c5','pt1:pc1:md1:c6','pt1:pc1:md1:c7','pt1:pc1:md1:c8','pt1:pc1:md1:c9','pt1:pc1:md1:c10','pt1:pc1:md1:c11','pt1:pc1:md1:c12']" _afrfilterable="true" _afrautohr="10" _afrhcc="0" _afrpcid="pt1:pc1" tabindex="0" style="height: 234px;"> 
    <div id="pt1:pc1:md1::ch" class="xz5" _afrcolcount="12" style="overflow: hidden; position: relative; width: 753px;"> 
    <div id="pt1:pc1:md1::db" class="xyy" _afrcolcount="12" style="position: relative; width: 753px; overflow: hidden; height: 170px; z-index: 1;"> 
    <table class="xyz xzr" cellspacing="0" _startrow="10" _rowcount="44" style="table-layout:fixed;position:relative;width:1260px;"> 
    <tbody> 
    <tr class="p_AFFocused p_AFSelected xzn" _afrrk="10"> 
    <tr class="xzn" _afrrk="11"> 
    <tr class="xzn" _afrrk="12"> 
    <tr class="xzn" _afrrk="13"> 
    <tr class="xzn" _afrrk="14"> 
    <tr class="xzn" _afrrk="15"> 
    <tr class="xzn" _afrrk="16"> 
    <tr class="xzn" _afrrk="17"> 
    <tr class="xzn" _afrrk="18"> 
    <tr class="xzn" _afrrk="19"> 
    </tbody> 
    </table> 
    <table class="xyz xzr" cellspacing="0" _startrow="20" _rowcount="44" style="table-layout:fixed;position:relative;width:1260px;"> 
    </div> 
    <div id="pt1:pc1:md1::sm" class="xzu" style="position: absolute; display: none; z-index: 5000; visibility: visible; top: 120px; right: 25px;">Fetching Data...</div> 
    <div id="pt1:pc1:md1::ri" class="xz0" style="position:absolute;display:none;overflow:hidden"></div> 
    <div id="pt1:pc1:md1::dataW" style="display:none"></div> 
    <div id="pt1:pc1:md1::scroller" tabindex="-1" style="position: absolute; overflow: auto; z-index: 0; width: 770px; top: 46px; height: 187px; right: 0px;"> 
    </div> 
    </div> 
    <div id="pt1:pc1::_ahBt" style="height:auto"> 
    <div id="pt1:pc1:_clmCxt" style="display:none"> 
    <div id="pt1:pc1:_PCPop" style="display:none"> 
    <div id="pt1:pc1::_dchDlgC" style="display:none"> 
    </div> 
    </div> 
    </div> 
    </div> 
    <div id="pt1:ph2" class="x6v"> 
    </span> 
    </div> 
    </div> 
+0

了XPath太路具體。複製/粘貼表格的HTML。 – Arran

+0

添加了HTML源代碼。 – gSr

+0

@gSr您的表格行具有「_afrrk」屬性,看起來像實際的** row_number **。如果這是真的,則使用此屬性作爲元素的ID。 –

回答

0

嘗試使用這種方法: 1)動作建設者,先進的用戶API MoveMouseAction方法尤其是 - 移動從當前位置將鼠標移動到另一個元素。 你可以得到here更多信息有關的行動構建器

2)也不要忘記使用fluent wait直到網頁元素呈現

public WebElement fluentWait(final By locator){ 
     Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) 
       .withTimeout(30, TimeUnit.SECONDS) 
       .pollingEvery(5, TimeUnit.SECONDS) 
       .ignoring(NoSuchElementException.class); 

     WebElement foo = wait.until(
new Function<WebDriver, WebElement>() { 
      public WebElement apply(WebDriver driver) { 
         return driver.findElement(locator); 
       } 
       } 
); 
          return foo;    }  ; 

By xpathElem=By.xpath("html/body/div[2]/form/div[1]/div[2]/div/div[2]/div/div[2]/div/div[2]/div/div[2]/div/span/div[1]/div/div/div/div[2]/div/div[2]/table/tbody/tr[1]/td[1]"); 
//getting data from cells 
fluentWait(xpathElem).getText().trim(); 

希望這在某種程度上可以幫助你;-)

+0

使用MoveMouseAction我可以滑動表格,我能夠做,但隨後滾動的記錄在運行時呈現,如何讀取它們? – gSr

+0

使用流利的等待,你很容易就可以獲取記錄呈現,並使用getText()你可以從表記錄中獲取文本。它適合你嗎? –

+0

感謝您的回答,滾動表格的xpath/id變化後,我寫在頂部。所以即使使用流暢的等待,它也會等待記錄被渲染,ID/Xpath是不同的,所以不能被識別。 – gSr

相關問題