环境:python + selenium + chromedriver.exe
问题:
用find_element_by_class_name在定位有多空格的复合类是会报错找不到。Compound class names not permitted.
selected_div = driver.find_element_by_class_name('next-pagination next-pagination-normal next-pagination-medium medium pagination')
方法1:
在获取包含多个class名称的tag对象时,建议使用:
find_element_by_css_selector(".xx.xxx.xxxxx")
或者
find_element_by_css_selector("[class='xx xxx xxxxx']")
又或者
find_element_by_xpath('//*[@class="mb20 clearfix answer_area"]/span[2]') 这种方法还可以找到复合类节点的子节点元素。
方法2:
class属性有空格是多个属性了,那定位的时候取其中的一个class就行(并且要唯一),可以按F12查找看复合类中的是否唯一。
如:class="a b c d e" ,
用find_element_by_class_name("a")也是可以获取到定位的
总之定位方式多种多样,得根据网页具体情况选择方便的定位方式。