#!/usr/bin/env python
# -*- coding:utf-8 -*-from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keysfrom selenium.common.exceptions import TimeoutExceptionimport timeimport redriver = webdriver.Chrome()
driver.get('http://www.baidu.com')input = driver.find_element_by_id('kw')
input.send_keys('绵阳市男科医院哪家好')input.send_keys(Keys.ENTER)wait = WebDriverWait(driver,3)def find(wait): #控制显示页数 page = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#page > strong > span.pc'))) p = int(page.text) if p == 3: print(p) return try: content = wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT,'www.lovesina.com'))) print(content) content.click() except TimeoutException: but = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.n:last-child'))) but.click() find(wait)find(wait)