When you open a webpage in Chrome and inspect an element, when you select the element and right click again you can choose to copy the CSS selector and XPath selector. I have a project that basically needs to automatically generate the CSS selector and XPath selector based on the given HTML page, so I am wondering what kind of rules Chrome is following on generate the CSS selector and XPath.
I know Chrome generates several kinds of CSS selector. Sometimes it will use nth-child
after the tag name to specify, and other times it will use class name after tag name, so which one should I choose automatically?
e.g:
html > body > div:nth-child(2) > div > div:nth-child(2) > p > a
vs
body > div.custom > div > div.master-body.col-md-10.col-sm-11.col-xs-12 > p > a.button
What’s the difference between these two and nth-child
, which seems easy to break if there are other elements with a different tag inside?
one
two
but not working forone
two
– Wu Xun Feb 16 '17 at 19:00