selenium 自动化点击页面
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
import time
import re
driver = 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)