我使用Selenium 2.12。使用WebDriver API,假設我有一個代表表格的WebElement(<table>
)。使用該元素,我如何選擇第一列中的所有td?我假設這裏有一個xpath表達式。如何選擇Selenium webdriver中的所有列第一個單元格(tds)?
爲了更好地瞭解表結構,如果我從我的tableElement WebElement ...
String html = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", tableElt);
得到HTML我得到下面的混亂。要注意的關鍵問題是,有6周TR的16 TD的...
<thead><tr><th colspan="1" class="GCSPOWVGE GCSPOWVEE GCSPOWVEF GCSPOWVFF"><div style="padding-left: 17px;position:relative;zoom:1;"><div style="left:0px;margin-top:-4px;position:absolute;top:50%;line-height:0px;"><img onload='this.__gwtLastUnhandledEvent="load";' src="http://localhost:9080/cme-productplus-web/productplus/clear.cache.gif" style="width: 11px; height: 7px; background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAAHCAYAAADebrddAAAAiklEQVR42mNgwALyKrumFRf3iDAQAvmVXVVAxf/zKjq341WYV95hk1fZ+R+MK8C4HqtCkLW5FZ2PQYpyK6AaKjv/5VV1OmIozq3s3AFR0AXFUNMrO5/lV7WKI6yv6mxCksSGDyTU13Mw5JV2qeaWd54FWn0BRAMlLgPZl/NAuBKMz+dWdF0H2hwCAPwcZIjfOFLHAAAAAElFTkSuQmCC) no-repeat 0px 0px;" border="0"></div><div>GUID</div></div></th><th colspan="1" class="GCSPOWVGE GCSPOWVEF">Fung Ratio</th><th colspan="1" class="GCSPOWVGE GCSPOWVEF">Fung type</th><th colspan="1" class="GCSPOWVGE GCSPOWVEF">Fung Date Offset</th><th colspan="1" class="GCSPOWVGE GCSPOWVEF GCSPOWVOE">Days To Retain</th></tr></thead><colgroup><col><col><col><col><col></colgroup><tbody><tr onclick="" class="GCSPOWVAE"><td class="GCSPOWVPD GCSPOWVBE GCSPOWVCE"><div style="outline:none;" tabindex="0"> DSSUAQR6IE6E </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;"> 10 </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;"> </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;"> </div></td><td class="GCSPOWVPD GCSPOWVBE GCSPOWVME"><div style="outline:none;"> </div></td></tr><tr onclick="" class="GCSPOWVAF"><td class="GCSPOWVPD GCSPOWVBF GCSPOWVCE"><div style="outline:none;"> ETTUAQR6IE6E </div></td><td class="GCSPOWVPD GCSPOWVBF"><div style="outline:none;"> 30 </div></td><td class="GCSPOWVPD GCSPOWVBF"><div style="outline:none;"> </div></td><td class="GCSPOWVPD GCSPOWVBF"><div style="outline:none;"> </div></td><td class="GCSPOWVPD GCSPOWVBF GCSPOWVME"><div style="outline:none;"> </div></td></tr><tr onclick="" class="GCSPOWVAE"><td class="GCSPOWVPD GCSPOWVBE GCSPOWVCE"><div style="outline:none;"> FCCUAQR6IE6E </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;"> 20 </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;"> </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;"> </div></td><td class="GCSPOWVPD GCSPOWVBE GCSPOWVME"><div style="outline:none;"> </div></td></tr></tbody><tbody style="display: none;"><tr><td colspan="5" align="center"><div><div style="width: 100%; height: 100%; padding: 0px; margin: 0px; display: none;"><div style="width: 100%; height: 100%; display: none;"></div></div><div style="width: 100%; height: 100%; padding: 0px; margin: 0px; display: none;"><div class="GCSPOWVPE" style="width: 100%; height: 100%; display: none;"><img class="gwt-Image" onload='this.__gwtLastUnhandledEvent="load";' src="http://localhost:9080/cme-productplus-web/productplus/clear.cache.gif" style="width: 43px; height: 11px; background: url(data:image/gif;base64,R0lGODlhKwALAPEAAP///0tKSqampktKSiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAKwALAAACMoSOCMuW2diD88UKG95W88uF4DaGWFmhZid93pq+pwxnLUnXh8ou+sSz+T64oCAyTBUAACH5BAkKAAAALAAAAAArAAsAAAI9xI4IyyAPYWOxmoTHrHzzmGHe94xkmJifyqFKQ0pwLLgHa82xrekkDrIBZRQab1jyfY7KTtPimixiUsevAAAh+QQJCgAAACwAAAAAKwALAAACPYSOCMswD2FjqZpqW9xv4g8KE7d54XmMpNSgqLoOpgvC60xjNonnyc7p+VKamKw1zDCMR8rp8pksYlKorgAAIfkECQoAAAAsAAAAACsACwAAAkCEjgjLltnYmJS6Bxt+sfq5ZUyoNJ9HHlEqdCfFrqn7DrE2m7Wdj/2y45FkQ13t5itKdshFExC8YCLOEBX6AhQAADsAAAAAAAAAAAA=) no-repeat 0px 0px;" border="0"></div></div></div></td></tr></tbody><tfoot style="display: none;"><tr><th colspan="5" class="GCSPOWVFE GCSPOWVDE GCSPOWVNE"></th></tr></tfoot>
可悲的是,這兩個表達式不產生正確的結果。
// This returns zero td's
final List<WebElement> tds = tableElt.findElements(By.xpath("/tr/td[1]"));
...
// This returns 238 td's (I think that's everything in my document)
final List<WebElement> tds = tableElt.findElements(By.xpath("//td[1]"));
什麼表達式我需要寫,如果我有一個已經p的WebElement有問題的桌子上有點油?所以,這將是類似tableElement.findElement(By.xpath(「...」))謝謝, - Dave – Dave
@Dave - 在這種情況下,它可能類似'/ tr/td [1]'或'// td [1]',具體取決於表結構 –
嗨,可悲的是每個表達式都沒有得到正確的結果。我編輯我的響應,以包含WebElement中的表結構。雖然這是一團糟,但我認爲第一個表達式(/ tr/td [1])在調用時返回零個元素,「最終列表 tds = tableElt.findElements(By.xpath(」/ tr/td [1] 「));」。第二個返回238,這是我的HTML文檔中的每個td,而不僅僅是table元素中的td。 –
Dave