Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
98
rated 0 times [  104] [ 6]  / answers: 1 / hits: 21759  / 11 Years ago, mon, september 2, 2013, 12:00:00

Using selenium i'm trying to read out a dynamically generated table, i got down to the right elements (using the findElement method) but using getText() on them returns nothing.



Probably because getText() looks for quotation marks when returning text and can't find any between the <td> tags. Some suggestions were to use xpaths but since the tables are generated dynamically the location of the value i need also changes.



here's the table i'm trying to get 3 data points from:



<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>&nbsp;</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>


the ones i'm interested in are the ones inside the <td class=value> tags but as i said before, using getText() on them returns null.



Any idea on how i can acces those values?



edit: here's how i'm doing it now



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;
}


edit: here's the relevant bit of 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&amp;galaxy=5&amp;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&amp;return=9965869 title=Roep terug:| 04.09.2013&lt;br&gt;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>&nbsp;</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&lt;br&gt;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&amp;galaxy=1&amp;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>


the values i need are the numeric values under Metaal, kristal and deuterium.


More From » java

 Answers
9

I found some kind of an answer after digging through a ton of programming sites.
Apparently items that have the style=display:none; flag set like this will be seen as hidden by selenium.



This is, unfortunately, a feature and not a bug of some sort as selenium tries to emulate the user and will therefore hide information that is not explicitely visible.



The user can't see it so neither can selenium is their thought progress.



Here is the source.



This, unfortunately, does not solve my issue. I can however try to get around this, I'll report back my findings.



EDIT: It's amusing how different HtmlUnitDriver is from FirefoxDriver. One thing that works on HtmlUnitDriver won't work on FirefoxDriver and vice versa.



EDIT-2: Found a solution! Finally.



Using selenium's JavascriptExecutor I can directly get the innerHTML from the found element like so:



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));


It's still annoying as hell that you can't just use getText() though. It should at least be an option.


[#75950] Saturday, August 31, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kieraelsies

Total Points: 718
Total Questions: 103
Total Answers: 104

Location: England
Member since Sun, May 21, 2023
1 Year ago
kieraelsies questions
Tue, Aug 3, 21, 00:00, 3 Years ago
Tue, Feb 23, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Mon, Sep 16, 19, 00:00, 5 Years ago
;