以百度搜索为例,搜索关键字“自动化测试”,验证搜索结果中是否包含关键字
from seleniumimport webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditionsas EC
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
input = driver.find_element_by_id('kw')
input.send_keys('自动化测试')
driver.find_element_by_id('su').click()
#searlist = driver.find_elements_by_xpath('//div/h3')
searlist = WebDriverWait(driver, 5, 0.5).until(EC.visibility_of_all_elements_located((By.XPATH,'//div/h3')))
for result in searlist:
text = result.find_element_by_tag_name('a').text
if '自动化测试' in text:
print('pass')
driver.quit()