Mahesh
Mahesh

Reputation: 1333

Get all links in a web page with xpath condition

For a give web page, I am able to extract all links in it by using document.links. However, I want to exclude the links which contains href="javascript:void(0)"

I am trying to exclude such links using xpath like this document.links.evaluate("//a[not(@href='javascript:void(0)')]", document) but unable to filter it out.

Please suggest a workaround

enter image description here

Upvotes: 0

Views: 646

Answers (2)

mplungjan
mplungjan

Reputation: 178026

If you want to be sure to test whatever is in the link, you can filter first

const links = [...document.querySelectorAll("a")]
  .filter(lnk => !lnk.href.includes("javascript:"))
  .map(lnk => lnk.href)

console.log(links)
<a href="javascript:void(0)">Link1</a>
<a href="https://google.com">Link2</a>
<a href="javascript:void(0)">Link3</a>
<a href="https://mdn.com">Link4</a>

Upvotes: 1

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196002

You should use CSS directly

const links = document.querySelectorAll('a:not([href="javascript:void(0)"])');

console.log(links.length)
<a href="something">something</a>
<a href="http://some.where">some.where</a>
<a href="javascript:void(0)">void</a>
<a href="https://somewhere.else">somewhere.else</a>

Upvotes: 2

Related Questions