Jsoup get CSS selector for any DOM element example shows how to get CSS selector for any DOM element using Google Chrome browser.
How to get CSS selector for any DOM element using Chrome?
CSS selectors are very useful especially if you are using HTML parser library like Jsoup. It becomes very difficult to determine the exact selector while trying to parse or scrap a complex website having tons of nested HTML elements. That is where Google Chrome becomes handy.
You do not need to spend time trying to understand the structure of the complex HTML. Here is how to do it using Google Chrome in under a minute.
Open the webpage you are trying to parse or scrap in Google Chrome.
Right click on the element for which you want the CSS selector and select “Inspect” menu item.
In the developer tools window, right click again on the selected element’s source, select “Copy” and then select “Copy selector” menu item.
Paste the copied CSS selector in your program. Here is the CSS selector value which Chrome gave us for the selected link element.
#rso > div:nth-child(1) > div:nth-child(2) > div > h3 > a
Similarly, you can also generate XPATH by selecting Copy > Copy XPath menu item.
Please let us know your views in the comments section below.