I'm trying to use the importxml function on Google Sheets on this page:


Essentially I want to import the "Forecast" bit that's down the page a little. I have been using this formula:

=importxml("https://www.attheraces.com/racecard/Ayr/15-February-2022/1320", "//*[@id="forecast"]/p[2]/strong")

here is a picture of the developer tools:


I assumed that I could just copy the XPath and add it to my URL, but it doesn't appear to be working.

Any help is appreciated.

1 Answer 1

  • Use //*[@id="forecast"]/p[2] if you want the word "forecast" as well as the forecast data,
  • Use //*[@id="forecast"]/p[2]/text() if you just want the forecast data (though it includes a blank row)

Your xPath "//*[@id="forecast"]/p[2]/strong" includes the strong attribute which adds nothing to define the location of the data or the data itself.

XPATH & Double and Single quotes

  • If IMPORTXML is referencing a cell value (such as =importxml(B1,B2) where the value is Cell B2 is //*[@id="forecast"]/p[2] then IMPORTXML will parse the XPath with no problem. BUT

  • If IMPORTXML contains an actual XPath value included in double quotes (such as =importxml("https://www.attheraces.com/racecard/Chelmsford-City/17-February-2022/1700","//*[@id="forecast"]/p[2]")', there will be a parse error. This is because there IMPORTXML` doesn't expect more than the opening/closing set of double quotes. In this case, change any other sets of double quotes to single quotes.

    • =importxml("https://www.attheraces.com/racecard/Chelmsford-City/17-February-2022/1700","//*[@id='forecast']/p[2]")



Forecast p2


forecast text

URL and Xpath in IMPORTXML #1 forecast01

URL and Xpath in IMPORTXML #2 forecast02

  • Thanks for your answer, but I can't get these to work unfortunately.
    – Klitiko
    Commented Feb 16, 2022 at 23:27
  • Refer my updated answer.
    – Tedinoz
    Commented Feb 17, 2022 at 1:10
  • Thank you so much, this is perfect!
    – Klitiko
    Commented Feb 17, 2022 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.