Templates, guides & honest tool reviews for small business

Sunday, November 29, 2026

IMPORTXML Function in Google Sheets: XPath Examples and Error Fixes

IMPORTXML function in Google Sheets
Illustration: IMPORTXML function in Google Sheets

The IMPORTXML function in Google Sheets pulls a specific piece of a web page into a cell, using an XPath query. The formula is =IMPORTXML("https://example.com/page", "//h1"), which would return the main heading of that page. Use it when IMPORTHTML does not fit, for example to get a title, a price or a list of links. This guide explains the syntax, simple XPath queries, how to find the right path, and why you often see errors. The URL in the examples is a placeholder.

IMPORTXML syntax

=IMPORTXML(url, xpath_query, [locale])

  • url is the full address, in quotes, with http:// or https://.
  • xpath_query is the path that selects the part of the page you want.
  • locale is optional and sets the language and region used to read the data.
IMPORTXML syntax in Google Sheets
URL, XPath, locale (illustration).

Simple XPath queries to start with

  • "//h1" returns the text of every h1 heading.
  • "//title" returns the page title.
  • "//a/@href" returns the address of every link.
  • "//li" returns the text of every list item.
  • "//h2" returns all h2 headings, one per row.

Example: =IMPORTXML(A1, "//h2") lists the h2 headings of the page in A1.

Simple XPath queries for IMPORTXML
Headings, titles and links (illustration).

Find an XPath for a specific item

Open the page in Chrome, right click the item and choose Inspect. In the developer panel, right click the highlighted line, then Copy, then Copy XPath. Paste it into the formula between quotes. If the path contains double quotes, such as //*[@id="price"], change the inner quotes to single quotes: //*[@id='price'].

The path Chrome gives is often very long and fragile. A shorter path that uses a class or id is more reliable, but still can break when the site changes its layout.

Copy an XPath from Chrome for IMPORTXML
Inspect, then Copy XPath (illustration).

Put the URL in a cell

Keep the page address in A1 and the query in B1, then use =IMPORTXML(A1, B1). You can test many queries without editing the formula.

Clean the result

Results spill into the cells below. To get one value only, wrap it in INDEX: =INDEX(IMPORTXML(A1, "//h2"), 1) returns the first heading. To trim spaces use TRIM, and to count how many items came back use COUNTA. If you need to filter the rows, QUERY works on the result.

IMPORTXML or IMPORTHTML?

IMPORTHTML reads whole tables and lists by their number on the page. IMPORTXML reads any element you can describe with a path. Our guide to the IMPORTHTML function covers the first one.

Why you get errors

  • "Imported content is empty." The path matched nothing, or the data is added by JavaScript after the page loads. IMPORTXML only sees the page source.
  • "Could not fetch URL." The page needs a login, blocks automated requests, or the address is wrong.
  • "Resource at url contents exceeds maximum size." The page is too large for the function.
  • #REF! "Array result was not expanded". Cells in the way are not empty.
  • Quotes problem. A double quote inside the path ends the text early. Use single quotes inside.
Fix IMPORTXML errors in Google Sheets
Common messages and causes (illustration).

Limits and good practice

Sites can change layout without notice, which breaks your path. Google also limits how many import functions a spreadsheet can run, and refreshes them on its own schedule, not on demand. Check the site's terms before you collect its data, and do not rely on it for numbers that must be exact. For data from your own files, see IMPORTRANGE.

Questions and answers

Does IMPORTXML work in Excel?

No. Excel has a WEBSERVICE and FILTERXML pair on Windows, and Power Query under Data, From Web, for the same kind of task.

Can I import a price from a shop page?

Sometimes. If the price is in the page source, a path to it works. Many shops load prices with scripts, which returns empty.

Why does my formula work today and fail tomorrow?

The site may have changed its page layout, started blocking requests, or the import limit was reached.

Can I import from an XML or RSS feed?

Yes. IMPORTXML reads XML, HTML, CSV, TSV, RSS and Atom feeds. For an RSS feed, a path such as "//item/title" returns the item titles.

العربية