XPath Cheatsheet for Search Engine Optimisation (SEO)#
Certain XPath expressions are especially useful for search engine optimisation.
Content#
H1 Headline#
Count all <h1> nodes to ensure that a web page has and only has one main headline. Should not be 0 or larger than 1:
count(//h1)
Example
How to use Browserist to check whether a web page has exactly one <h1> headline tag:
| Python | |
|---|---|
1 2 3 4 5 | |
Image Alt Text#
Ensure that all images on web page have an alt text.
//img[not(@alt)]
Example
How to use Browserist to check whether a web page has images without alt text:
| Python | |
|---|---|
1 2 3 4 5 6 7 8 | |
Meta Data#
Similarly, ensure that a web page has meta description. Should not be 0 or larger than 1:
count(//meta[@name='description']/@content)
And you can ensure that length of the meta description is between 50 and 160 characters by using the string-length() function:
string-length(/html/head/meta[@name='description']/@content) >= 50
string-length(/html/head/meta[@name='description']/@content) <= 160
Check if a web page has a canonical URL. If it has, it should only be 1:
count(//link[@rel='canonical'])
Ensure that a web page has a robots meta tag. Should not be 0 or larger than 1:
count(//meta[@name='robots'])
Example
How to use Browserist to check whether a web page has relevant meta data:
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 | |
Page Title#
Ensure that a web page doesn't have an empty title:
/html/head/title[. != '']
Example
How to use Browserist to check whether a web page has a page title:
| Python | |
|---|---|
1 2 3 4 5 | |
Alternatively, there's a simpler solution without using XPath:
| Python | |
|---|---|
5 | |