Selenium:无法通过XPath抓取文本

时间:2016-07-28 20:13:13

标签: xml selenium xpath selenium-webdriver web-scraping

我试图抓文:

  

10小时51分钟

来自以下HTML代码段:

<div class="a-box-inner">
<div class="a-row a-spacing-mini prime-ad-banner-content" data-testid="">
<div class="a-row shipment" data-testid="order-box-0" data-orderid="0">
<div class="a-row">
<div class="a-row shipping-group">
<div class="a-row" data-testid="">
<div class="a-row a-color-success a-size-medium">
<span class="a-text-bold" data-promisetype="delivery">Guaranteed delivery date:</span>
<span class="a-color-success a-text-bold">
<span class="a-size-base a-color-secondary fasttrack-span hidden a-text-normal" style="display: inline;">
<span class="fasttrackexpired hidden" style="display: none;">
<span class="fasttrackavailable fasttrackcountdown hidden a-text-normal" style="display: inline;">
If you order in the next
<span data-field="fasttrackcountdown">10 hours and 51 minutes</span>
(
<a class="a-size-mini" href="/gp/help/customer/display.html/ref=chk_ship_ft_details_pri?ie=UTF8&nodeId=3510241" target="AmazonHelp">Details</a>
)
</span>
<div id="a-popover-" class="a-popover-preload">
<div id="a-popover-" class="a-popover-preload">
<input type="hidden" value="39399" name="fasttrackExpiration">
<input type="hidden" value="0" name="countdownThreshold">
<input type="hidden" value="0" name="showSimplifiedCountdown">
<input type="hidden" value="countdownId-0" name="countdownId">
</span>
</div>
</div>
<div class="a-row a-spacing-small">
<div class="a-row">

我正在使用XPath:

.//*[@id='spc-orders']/div[1]/div/div[2]/div/div/div[1]/div/span[3]/span[2]/span

然而,尽管我能够使用Firebug和Eclipse识别这个元素 - 当我尝试在这个元素上使用getText时,我得不到任何回报。换句话说,我无法删除之前提到的值。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

This XPath

//span[@data-field='fasttrackcountdown']

will select this element

<span data-field="fasttrackcountdown">10 hours and 51 minutes</span>

in your HTML, as requested.

答案 1 :(得分:0)

正如您所说的xPath是正确的那么可能是时间,当您要查找元素时,可能不会出现文本,您应该尝试使用WebDriverWait等待直到元素可见cssSelectorspan[data-field='fasttrackcountdown']如下(假设您使用的是Java): -

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("span[data-field='fasttrackcountdown']")));
el.getText();

如果您仍然无法找到文字,则应使用getAttribute("innerHTML"),如下所示: -

el.getAttribute("innerHTML");

或尝试使用getAttribute("textContent"),如下所示: -

el.getAttribute("textContent");

希望它有帮助...:)