ee clipse
ee clipse

Reputation: 245

Selenium: Unable to Scrape Text Via XPath

I'm trying to scrape the text:

10 hours and 51 minutes

from the following HTML snippet:

<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">

I'm using the XPath:

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

However although I'm able to identify this element using Firebug as well as with Eclipse - when I try using getText on this element I get nothing in return. In other words, I'm unable to scraped previous mentioned values.

Any ideas?

Upvotes: 3

Views: 360

Answers (2)

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

As you are saying your xPath is correct then It might be timing, at the time when you're going to find element, may be it wouldn't present with text, you should try using WebDriverWait to wait until element visible with cssSelector as span[data-field='fasttrackcountdown'] as below(Assuming you are using Java) :-

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

If still you are not able to find text, you should use getAttribute("innerHTML") as below :-

el.getAttribute("innerHTML");

Or try using getAttribute("textContent") as below :-

el.getAttribute("textContent");

Hope it helps...:)

Upvotes: 0

kjhughes
kjhughes

Reputation: 111521

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.

Upvotes: 1

Related Questions