selenium抓取元素排除某个特定的class标签

排除某个因素,第一优选想到正则表达式,无奈折腾半天没有成功,感觉是selenium对元素的attrs按re search在操作,$对字符串末尾检测都没什么用。

BeautifulSoup可以用element[‘class’]输出元素的class进行检测,但是BeautifulSoup对象不能再进行click操作,不符合期望。selenium没有这样的语法,通过xpath进行选择:

itemList = driver.find_elements_by_xpath('//div[@id = "choose-color"]//div[@class = "dd"]//div[not(contains(@class, "disabled"))]')

stackoverflow有个参考:http://stackoverflow.com/questions/11024080/how-to-use-not-contains-in-xpath


补充xpath用法:

xpath=xpathExpression: Locate an element using an XPath expression.  
  
xpath=//img[@alt=’The image alt text’]  
xpath=//table[@id=’table1’]//tr[4]/td[2]  
xpath=//a[contains(@href,’#id1’)]  
xpath=//a[contains(@href,’#id1’)]/@class  
xpath=(//table[@class=’stylee’])//th[text()=’theHeaderText’]/../td  
xpath=//input[@name=’name2’ and @value=’yes’]  
xpath=//*[text()=”right”]


你可能感兴趣的:(爬虫,python)