javascript - Alternative to nth-child approach in automatic css selector generation -
i facing problem while making logic automatic generation of css path of element in web page.
use-case: when user clicks on element on web page, css selector generated automatically using css selector concepts id-name, class-name, tag-name, , nth-child.
for ex: lets click on price element on e-commerce product description page, body > ul:nth-child(27) > li:nth-child(1)
. biggest bane here nth-child(1)
, because page price 1st child, other pages 4th or 5th or 23rd child. moreover, bigger problem html of price element
<html> .... <body> <div>abc</div> <div>def <span>ghi</span> <ul> <li> price: $32.23 </div> <li> weight: 100 lbs </div> </ul> </div> </body> </html>
without classname or id-name. current selector generation logic generates lot of combinations of selectors selects selector unique.
please me!
<span class='product'>cheese</span> <ul class='product-details' > <li class='price' id='cheese-price'> price: $32.23 </li> <li class='weight' id='cheese-weight'> weight: 100 lbs </li> </ul> <span class='product'>apple</span> <ul class='product-details' > <li class='price' id='apple-price'> price: $2.23 </li> <li class='weight' id='apple-weight'> weight: 1 lbs </li> </ul>
my concept add classes classify , make unique ids based on product name , -price
or -weight
suffixes, ex.: cheese-price
. might use jquery functions identify elements class or end of attribute name: -price
because page price 1st child, other pages 4th or 5th or 23rd child.
if click on price: $23.32, js function read textcontent inside
li
elementi click on price element on e-commerce product description page
...but no database in involved here. web page , dom
do generate css selectors on server side or thru js function
upon user click? if later, in wrong sphere.
update
since try add css selectors on client side (after receiving html page), suggestion grab hole page, save locally , process regex based on words price
, weight
. ex.: /price:\s+\$([\.\d]+)/g
see working demo.
Comments
Post a Comment