How to write conditional xpath selection for specific nodes?

So I have custom text added to chrome, autopagerizer.

I am currently using

//div[@class=\'entry-content\']/p[contains(.,\'chapter\') or contains(.,\'Chapter\') or preceding-sibling::p[contains(.,\'Chapter\')] and (following-sibling::p[contains(.,\'Translator Ramblings\')] or following-sibling::p[contains(.,\'Translating Ramblings\')] or following-sibling::p[contains(.,\'Ramblings\')] or following-sibling::p[starts-with(.,\'Translator\')] or following-sibling::p[starts-with(.,\'Translating\')])] 

      

I want a conditional argument where when the above code doesn’t allow anything it selects

//h3[contains(.,\'Share this\')]

      

Instead. I want h3 to be selected when this is the case. I cannot figure out how to write this in xpath, I looked at other related questions and they do not solve this problem. If I cannot solve this with xpath, how can I solve it with javascript? I use chrome monkey grease.

+3


source to share


1 answer


Basically, a pattern to achieve an explanation using pure XPath 1.0 could be as follows:

expression1 | expression2[not(expression1)]

      

So, in the context of your question:



  • expression1 (truncated): //div[@class=\'entry-content\']/p[...]

  • expression2: //h3[contains(.,\'Share this\')]

  • decision:

    //div[@class=\'entry-content\']/p[...]  | 
      //h3[contains(.,\'Share this\')]
          [not(//div[@class=\'entry-content\']/p[...])]
    
          

It can be much cleaner if implemented with Javascript. Just execute expression1 and if the result has length 0 then execute expression2.

+1


source







All Articles