Selenium里有一个excepted_conditions模块。我们经常要用到EC.title()或者EC.title_contains()来判定标题,疑惑的地方是在括号内放入什么内容,都会有返回结果,不直观!
1 from selenium import webdriver 2 from selenium.webdriver.support import expected_conditions as EC 3 4 options = webdriver.ChromeOptions() 5 options.add_argument('--headless') #无界面 6 7 url = 'https://www.baidu.com' #定义url 8 driver = webdriver.Chrome(options=options) #加载浏览器 9 driver.get(url) #请求网页 10 print(EC.title_contains(u'百度一下,你就知道')) #返回一窜字符 <selenium.webdriver.support.expected_conditions.title_contains object at 0x000002881A954A48> 11 print(EC.title_contains(u'百度一下,你就知道')(driver)) #返回True 12 print(EC.title_contains(u'川普是坨大便')) #返回一寸字符串 <selenium.webdriver.support.expected_conditions.title_contains object at 0x000001C06E494A08> 13 print(EC.title_contains(u'川普是坨大便')(driver)) #返回false
在判定的后面加上driver就会返回布尔值,这样就很直观了。