Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
LemonWiki共筆
Search
Search
Appearance
Log in
Personal tools
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Xpath tools
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Browser Console == === List matched elements by XPath rule === Open the browser console in {{chrome}} or {{Edge}}: # Menu: Tools → Developer Tools → Console # To verify elements matching an XPath rule, type the expression using $x(). For example, to find all images, enter {{kbd | key = <nowiki>$x("//img")</nowiki>}} and press Enter. The matched elements will be listed in the console<ref>[http://stackoverflow.com/questions/3030487/is-there-a-way-to-get-the-xpath-in-google-chrome Is there a way to get the xpath in google chrome? - Stack Overflow]</ref><ref>[https://yizeng.me/2014/03/23/evaluate-and-validate-xpath-css-selectors-in-chrome-developer-tools/ Evaluate and validate XPath/CSS selectors in Chrome Developer Tools | Yi Zeng's Blog]</ref>: https://planetoid.info/images/xpath_result_on_browser_console.jpg To extract and display the text content of matched elements — cleaned and separated by newlines — use the snippet below. The example uses XPath rule {{kbd | key = <nowiki>//div[@id="showvender"]/a</nowiki>}}: <pre> // Use document.evaluate to retrieve all matching elements const result = document.evaluate('//div[@id="showvender"]/a', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); // Collect text content from each element const textArray = []; for (let i = 0; i < result.snapshotLength; i++) { const element = result.snapshotItem(i); // Strip newlines and trim whitespace const cleanedText = element.textContent.replace(/[\n\r]+/g, '').trim(); textArray.push(cleanedText); } // Print all results, one per line console.log(textArray.join('\n')); </pre> === List matched elements by CSS selector === To quickly list all elements matching a CSS selector, use {{kbd | key = <nowiki>$$("css-selector")</nowiki>}} in the console. For example, to find all images, enter {{kbd | key = <nowiki>$$("img")</nowiki>}} and press Enter. The matched elements will be listed in the console. To extract and display the text content of matched elements — cleaned and separated by newlines — use the snippet below. The example uses CSS selector {{kbd | key = <nowiki>div#showvender a</nowiki>}}: <pre> // Use querySelectorAll to retrieve all matching elements const elements = document.querySelectorAll('div#showvender a'); // Collect text content from each element const textArray = []; elements.forEach(el => { // Strip newlines and trim whitespace const cleanedText = el.textContent.replace(/[\n\r]+/g, '').trim(); textArray.push(cleanedText); }); // Print all results, one per line console.log(textArray.join('\n')); </pre> === Highlight matched elements by XPath rule === By XPath rule: <pre> const xpath = '//main[contains(@class, "htmlview") and @data-cat="article"]'; // Helper: resolve XPath to an array of elements function $x(xpath) { const result = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); return Array.from({ length: result.snapshotLength }, (_, i) => result.snapshotItem(i)); } // Scroll to the first matched element $x(xpath)[0]?.scrollIntoView(); // Highlight all matched elements with a red outline $x(xpath).forEach(el => { el.style.outline = '3px solid red'; }); </pre> === Highlight matched elements by CSS selector === By CSS selector: <pre> const cssSelector = "main.htmlview[data-cat='article']"; // Scroll to the first matched element document.querySelector(cssSelector)?.scrollIntoView(); // Highlight all matched elements with a red outline document.querySelectorAll(cssSelector).forEach(el => { el.style.outline = '3px solid red'; }); </pre>
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki共筆:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)