XPath partial of attribute known

前端 未结 2 1981

I know the partial value of an attribute in a document, but not the whole thing. Is there a character I can use to represent any value? For example, a value of a label for

相关标签:
2条回答
  • 2020-12-29 06:10

    Found Ronald's solution while trying to find a way to test whether a node has either an attribute 'align' that is not empty OR an attribute 'style' that contains text value 'text-align'. These are the 'nodes' in question:

    <node> 
      <p>This is left-aligned.</p> 
      <div align="left" >This is aligned LEFT using HTML attribute.</div> 
      <p style="text-align: center;" >This is centered using CSS style attribute.</p> 
      <div align="center" >This is CENTERED.</div> 
      <p style="text-align: right;" >This is right-aligned.</p> 
    </this>
    

    This xpath expression worked -- thanks to Ronald's answer that pointed me in the right direction:

    //*[contains(@style, 'align') or @align!='']
    
    0 讨论(0)
  • 2020-12-29 06:28

    Your XPath expression should look like this:

    //td[contains(@label, 'Choice 1')]/input
    

    You select all td elements that have a label that contains Choice 1 and then you select the input elements inside these td elements.

    EDIT: Tomalak's comment correctly suggests an improvement to prevent a match against 'Choice 11' (or 'Choice 12345', ...).

    0 讨论(0)
提交回复
热议问题