简体   繁体   中英

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

在此处输入图片说明

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM