使用selenium我试图读出一个动态生成的表,我得到了正确的元素(使用findElement
方法),但在它们上使用getText()
则不返回任何内容。
可能是因为getText()
在返回“text”时查找引号,而在<td>
标记之间找不到任何引号。一些建议是使用xpath,但由于表是动态生成的,因此值的位置也需要更改。
这是我试图获得3个数据点的表:
<table cellpadding="0" cellspacing="0" class="fleetinfo">
<tbody><tr>
<th colspan="2">Schepen:</th>
</tr>
<tr>
<td>Groot vrachtschip:</td>
<td class="value">
40 </td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<th colspan="2">Lading:</th>
</tr>
<tr>
<td>Metaal:</td>
<td class="value">
536.062 </td>
</tr>
<tr>
<td>Kristal:</td>
<td class="value">
289.008 </td>
</tr>
<tr>
<td>Deuterium:</td>
<td class="value">
92.750 </td>
</tr>
</tbody></table>
我感兴趣的是<td class="value">
标签内的那些,但正如我之前所说,在它们上使用getText()
会返回null。
关于我如何获取这些价值的任何想法?
编辑:这是我现在正在做的事情
private int getMetalFromFleet(WebElement fleet)
{
int ret=0;
WebElement streak = fleet.findElement(By.className("starStreak"));
List<WebElement>fleetDetails = streak.findElements(By.tagName("tr"));
for(WebElement detail : fleetDetails)
{
List<WebElement> tabel = detail.findElements(By.tagName("td"));
if(tabel.size() != 2)
continue;
if(tabel.get(0).getText().equalsIgnoreCase("metaal:"))
{
ret = Integer.parseInt(tabel.get(1).getText());
break;
}
}
return ret;
}
编辑:这是html的相关位
<div id="fleet9965869" class="fleetDetails detailsOpened" data-mission-type="4" data-return-flight="false" data-arrival-time="1378241688">
<span class="timer tooltip" title="03.09.2013 22:54:48" id="timer_9965869">58m 48s</span>
<span class="absTime">22:54:48 Klok</span>
<span class="mission neutral textBeefy">Plaatsen</span>
<span class="allianceName"></span>
<span class="originData">
<span class="originCoords tooltip" title="killernerd"><a href="http://uni107.ogame.nl/game/index.php?page=galaxy&galaxy=5&system=213">[5:213:8]</a></span>
<span class="originPlanet">
<figure class="planetIcon planet tooltip js_hideTipOnMobile" title="planeet"></figure>k7 </span>
</span>
<span class="marker01"></span>
<span class="marker02"></span>
<span class="fleetDetailButton">
<a href="#bl9965869" rel="bl9965869" title="Vlootdetails" class="tooltipRel tooltipClose fleet_icon_forward">
</a>
</span>
<span class="reversal reversal_time" ref="9965869">
<a class="icon_link tooltipHTML" href="http://uni107.ogame.nl/game/index.php?page=movement&return=9965869" title="Roep terug:| 04.09.2013<br>01:54:05">
<img src="http://gf2.geo.gfsrv.net/cdna2/89624964d4b06356842188dba05b1b.gif" height="16" width="16">
</a>
</span>
<span class="starStreak">
<div style="position: relative;">
<div class="origin fixed">
<img class="tooltipHTML" height="30" width="30" src="http://gf1.geo.gfsrv.net/cdnf0/af41c52dc08208b4463f4a4608e88c.png" title="" alt="">
</div>
<div class="route fixed">
<a href="#bl9965869" rel="bl9965869" title="Vlootdetails" class="tooltipRel tooltipClose basic2 fleet_icon_forward" id="route_9965869" style="margin-left: 220px;"></a>
<div style="display:none;" id="bl9965869">
<div class="htmlTooltip">
<h1>Vlootdetails:</h1>
<div class="splitLine"></div>
<table cellpadding="0" cellspacing="0" class="fleetinfo">
<tbody><tr>
<th colspan="2">Schepen:</th>
</tr>
<tr>
<td>Groot vrachtschip:</td>
<td class="value">
960 </td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<th colspan="2">Lading:</th>
</tr>
<tr>
<td>Metaal:</td>
<td class="value">
8.173.484 </td>
</tr>
<tr>
<td>Kristal:</td>
<td class="value">
6.325.966 </td>
</tr>
<tr>
<td>Deuterium:</td>
<td class="value">
7.474.821 </td>
</tr>
</tbody></table>
</div> </div>
</div>
<div class="destination fixed">
<img class="tooltipHTML" height="30" width="30" src="http://gf2.geo.gfsrv.net/cdnaa/af0b356fdbecc1cfc47130e990fa66.png" title="Aankomsttijd:| 03.09.2013<br>22:54:48" alt="">
</div>
</div>
</span><!-- Starstreak -->
<span class="destinationData">
<span class="destinationPlanet">
<span>
<figure class="planetIcon planet tooltip js_hideTipOnMobile" title="planeet"></figure>Hoelbrak </span>
</span>
<span class="destinationCoords tooltip" title="killernerd"><a href="http://uni107.ogame.nl/game/index.php?page=galaxy&galaxy=1&system=2">[1:2:6]</a></span>
</span>
<span class="nextTimer tooltip" title="04.09.2013 03:52:31" id="timerNext_9965869">5u 56m 31s</span>
<span class="nextabsTime">03:52:31 Klok</span>
<span class="nextMission friendly textBeefy">Keer terug</span>
<span class="openDetails">
<a href="javascript:void(0);" class="openCloseDetails" data-mission-id="9965869" data-end-time="1378241688">
<img src="http://gf3.geo.gfsrv.net/cdnb6/577565fadab7780b0997a76d0dca9b.gif" height="16" width="16">
</a>
</span>
</div>
我需要的值是“Metaal”,“kristal”和“deuterium”下的数值。
答案 0 :(得分:2)
我建议在这种情况下使用xpath,而不是依赖于标记名;
private int getMetalFromFleet(WebElement fleet)
{
By identifier = By.xpath("td[contains(text(),'Metaal')]/following-sibling::td[contains(@class,'value')]");
return Integer.parseInt(fleet.findElement(identifier).getText());
}
显然你会想要一些错误处理,但希望这会让人觉得那些循环并不总是需要的,而且Xpath并不总是那些由一些人制作出来的怪物。
修改
万一有人感到困惑,如果选择器是在WebElement而不是WebDriver对象上执行的,那么XPath没有前导斜杠是可以的。
如果在WebDriver对象上使用了此选择器,则必须添加两个前导斜杠。
再次编辑!
Xpath可能需要一些调整,但你应该了解这种方法。我在你的html中看不到“StarStreek”,但它在你的代码中,所以将它添加到我的xpath中。
答案 1 :(得分:2)
在挖掘了大量的编程网站后,我找到了一些答案。
显然,设置style="display:none";
标志的项目将被selenium视为“隐藏”。
不幸的是,这是一个功能,而不是某种类型的错误,因为selenium“试图模仿使用 r”,因此会隐藏明显无法显示的信息。
“用户无法看到它,因此selenium ”也不是他们的思想进展。
以下是source.
遗憾的是,这并没有解决我的问题。然而,我可以尝试解决这个问题,我会报告我的发现。
编辑:来自FirefoxDriver的不同HtmlUnitDriver是多么有趣。在HtmlUnitDriver上运行的一件事在FirefoxDriver上不起作用,反之亦然。
EDIT-2 :找到解决方案!最后。
使用selenium的JavascriptExecutor
我可以直接从找到的元素中获取innerHTML,如下所示:
By identifier = By.xpath("*[contains(@class,'starStreak')]//td[contains(text(),'Metaal:')]/following-sibling::td[contains(@class,'value')]");
String script = "return arguments[0].innerHTML";
String outcome = (String) ((JavascriptExecutor) driver).executeScript(script, fleet.findElement(identifier));
尽管你不能只使用getText()
,但仍然很烦人。它应该至少是一种选择。
答案 2 :(得分:0)
有类似的问题 - 我想点击表中给定值的行。解决方案(id可以更改为类名等):
driver.findElement(By.xpath("//table[@id='<yourTableId>']//tr//td[contains(text(),'"+<givenValue>+"')]")).click();