How to get html elements with multiple css classes

邮差的信 提交于 2020-01-26 12:46:45

问题


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

<div class="class1">1</div>
<div class="class1">2</div>

using xpath //div[@class='class1']

But how if a div have multiple classes, e.g

<div class="class1 class2">1</div>

What will the xpath like then?


回答1:


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/




回答2:


i think this the expression you're looking for is

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



回答3:


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.



来源:https://stackoverflow.com/questions/3881044/how-to-get-html-elements-with-multiple-css-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!