Getting Element Path for selector

前端 未结 4 917
既然无缘
既然无缘 2020-12-16 04:59

Running into a spot of trouble and basically trying to create a variable which can be used as a selector. eg

$(\'a\').click(function(){
   var selector = $(t         


        
4条回答
  •  醉话见心
    2020-12-16 05:25

    You would need enumerate all parents of the element you want to create a query for, and add a selector for each parent, e.g. the node name of the parent or the name with the contains-test, if that test is needed for that parent. The only way to be sure, if this contains-test is needed, is probably to apply the current query against the current parent in each step and check, if the query only returns the target element. Then add the contains-test if it matches too much...

    I wrote a Greasemonkey script doing that. First it collects all elements that are needed to find the target element in another tree ("template") and then converts that to a query. However, it uses the attributes (specifically class/id/name ) instead of the text for matching, and the position, if the attributes are not unique enough, since I think in most cases the text changes more often than the structure.

提交回复
热议问题