使用 selenium 我试图读出一个动态生成的表,我找到了正确的元素(使用该findElement方法),但getText()在它们上使用什么也不返回。
可能是因为在返回“文本”时查找引号并且在标签getText()之间找不到任何引号。<td>一些建议是使用 xpaths,但由于表是动态生成的,所以我需要的值的位置也会发生变化。
这是我试图从中获取 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>
我需要的值是“金属”、“水晶”和“氘”下的数值。