How to get html elements with multiple css classes

前端 未结 4 1403
渐次进展
渐次进展 2020-11-28 06:57

I know how to get a list of DIVs of the same css class e.g

1
2
相关标签:
4条回答
  • 2020-11-28 07:25

    The expression you're looking for is:

    //div[contains(@class, 'class1') and contains(@class, 'class2')]
    

    I highly suggest XPath visualizer, which can help you debug xpath expressions easily. It can be found here:

    http://xpathvisualizer.codeplex.com/

    0 讨论(0)
  • 2020-11-28 07:30

    i think this the expression you're looking for is

    //div[starts-with(@class, "class1")]/text()

    0 讨论(0)
  • 2020-11-28 07:40

    According to this answer, which explains why it is important to make sure substrings of the class name that one is looking for are not included, the correct answer should be:

    //div[contains(concat(' ', normalize-space(@class), ' '), ' class1 ')
        and contains(concat(' ', normalize-space(@class), ' '), ' class2 ')]
    
    0 讨论(0)
  • 2020-11-28 07:44

    There's a useful python package called cssselect.

    from cssselect import CSSSelector CSSSelector('div.gallery').path

    Generates a usable XPath:

    descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' gallery ')]
    

    It's very similar to Flynn1179's answer.

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