I am basically in the same situation as this question Getting attribute using XPath and the solution in that question works for me in Chrome and Firefox BUT I have to use Safari (for a couple of different reasons, mainly AppleScript) and using /@
in Safari returns an empty result.
Are there alternative ways to extract xpath attributes?
They/it must be compatible with document.evaluate
. (I have a solution that works with $x
but it doesn't work with document.evaluate
, adding .map(link => link.href)
at the end of the xpath).
Update: it seems I am partially wrong - @/
does indeed work in all cases I have tried but mine.
More details to reproduce my problem: On https://www.facebook.com/zuck/followers I want to extract the link to all the followers. The element I target looks like this:
<a class="x1i10hfl xjbqb8w x6umtig x1b1mbwd xaqea5y xav7gou x9f619 x1ypdohk xt0psk2 xe8uvvx xdj266r x11i5rnm xat24cr x1mh8g0r xexx8yu x4uap5 x18d9i69 xkhd6sd x16tdsg8 x1hl2dhg xggy1nq x1a2a7pz x1heor9g xt0b8zv" href="https://www.facebook.com/profile.php?id=100065103331942" role="link" tabindex="0">
A simple xpath (I will optimise it later) that points to this element:
//div/div[1]/div/div[3]/div/div/div/div[1]/div[1]/div/div/div[4]/div/div/div/div/div/div/div/div/div[3]/div/div[2]/div[1]/a
As you can see the a has four attributes, class
, href
, role
and tabindex
.
If I add /@class
at the end of my xpath it evaluates to
class="x1i10hfl xjbqb8w x6umtig x1b1mbwd xaqea5y xav7gou x9f619 x1ypdohk xt0psk2 xe8uvvx xdj266r x11i5rnm xat24cr x1mh8g0r xexx8yu x4uap5 x18d9i69 xkhd6sd x16tdsg8 x1hl2dhg xggy1nq x1a2a7pz x1heor9g xt0b8zv"
just as expected. Adding role
or tabindex
as the end also gives the expected result but if I try with href
instead of the other three attributes, I just get href
(the actual string) as result. Or, it is even weirder and hard to describe in just text:
href
differently from other XML attributes? Can this really be true? Workarounds?Select the link elements with document.evaluate
, then extract the href
DOM properties as shown below:
var links = []; var xpathResult = document.evaluate('//div/div[1]/div/div[3]/div/div/div/div[1]/div[1]/div/div/div[4]/div/div/div/div/div/div/div/div/div[3]/div/div[2]/div[1]/a', document, null); var link = null; while ((link = xpathResult.iterateNext()) != null) { links.push(link.href); } ; links