动态数据需要手动刷新,或者直接分析接口,但是有的接口分析复杂度高,这个时候我们可以利用Selenium帮我们完成
Selenium相当于是一个机器人。可以模拟人类在浏览器上的一些行为,自动处理浏览器上的一些行为,比如点击,填充数据,删除cookie等。chromedriver是一个驱动Chrome浏览器的驱动程序,使用他才可以驱动浏览器。当然针对不同的浏览器有不同的driver。以下列出了不同浏览器及其对应的driver:
安装chromedriver:下载完成后,放到不需要权限的纯英文目录下就可以了。
python 直接pip下载
pip install selenium
selenium详细介绍
简单示例:打开一个网页
# -*- coding:utf-8 -*-
from selenium import webdriver
# chromedriver的绝对路径
driver_path = r'D:\ProgramApp\chromedriver\chromedriver.exe'
# 初始化一个driver,并且指定chromedriver的路径
driver = webdriver.Chrome(executable_path=driver_path)
# 请求网页
driver.get("https://www.baidu.com/")
# 通过page_source获取网页源代码
print(driver.page_source)
WebElement接口的值
关闭页面:
driver.close():关闭当前页面。
driver.quit():退出整个浏览器。
定位元素:
find_element_by_id:根据id来查找某个元素。等价于:
submitTag = driver.find_element_by_id(‘su’)
submitTag1 = driver.find_element(By.ID,‘su’)
find_element_by_class_name:根据类名查找元素。 等价于:
submitTag = driver.find_element_by_class_name(‘su’)
submitTag1 = driver.find_element(By.CLASS_NAME,‘su’)
find_element_by_name:根据name属性的值来查找元素。等价于:
submitTag = driver.find_element_by_name(‘email’)
submitTag1 = driver.find_element(By.NAME,‘email’)
find_element_by_tag_name:根据标签名来查找元素。等价于:
submitTag = driver.find_element_by_tag_name(‘div’)
submitTag1 = driver.find_element(By.TAG_NAME,‘div’)
find_element_by_xpath:根据xpath语法来获取元素。等价于:
submitTag = driver.find_element_by_xpath(’//div’)
submitTag1 = driver.find_element(By.XPATH,’//div’)
find_element_by_css_selector:根据css选择器选择元素。等价于:
submitTag = driver.find_element_by_css_selector(’//div’)
submitTag1 = driver.find_element(By.CSS_SELECTOR,’//div’)
要注意,find_element是获取第一个满足条件的元素。find_elements是获取所有满足条件的元素。
获取所有的cookie:
for cookie in driver.get_cookies():
print(cookie)
根据cookie的key获取value:
value = driver.get_cookie(key)
删除所有的cookie:
driver.delete_all_cookies()
删除某个cookie:
driver.delete_cookie(key)
# -*- coding:utf-8 -*-
from selenium import webdriver
driver_path = r"D:\python3.7\Chromedriver\chromedriver.exe"
driver = webdriver.Chrome(executable_path=driver_path,)
driver.get("http://www.baidu.com")
inputTag = driver.find_element_by_id('kw')
inputTag.send_keys('python') # 输入框输入内容
# 通过回车键来代替单击操作
driver.find_element_by_id("su").send_keys(Keys.ENTER)
inputTag.clear() # 清除输入框内容
# -*- coding:utf-8 -*-
from selenium import webdriver
import time
driver_path = r"D:\python3.7\Chromedriver\chromedriver.exe"
driver = webdriver.Chrome(executable_path=driver_path,)
driver.get("https://accounts.douban.com/passport/login_popup?login_source=anony")
rememberBtn = driver.find_element_by_class_name('account-form-remember')
rememberBtn.click() # 勾选自动登录
示例代码如下
# -*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver_path = r"D:\python3.7\Chromedriver\chromedriver.exe"
driver = webdriver.Chrome(executable_path=driver_path,)
driver.get('https://www.baidu.com/')
inputTag = driver.find_element_by_id('kw')
submitBtn = driver.find_element_by_id('su')
actions = ActionChains(driver)
actions.move_to_element(inputTag)
actions.send_keys_to_element(inputTag,'python')
actions.move_to_element(submitBtn)
actions.click(submitBtn)
actions.perform()
示例代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver_path = r"D:\python3.7\Chromedriver\chromedriver.exe"
driver = webdriver.Chrome(executable_path=driver_path,)
driver.get("http://www.baidu.com/")
input_Tag = driver.find_element_by_id('kw')
input_Tag .send_keys(Keys.TAB)
input_Tag .send_keys('python')
input_Tag .send_keys(Keys.ENTER) # 按下回车键(可代替单击)
input_Tag .send_keys(Keys.BACK_SPACE) # 按下删除键BackSpace
implicitly_wait() 隐式等待 相当于time.sleep()
WebDriverWait()显示等待 在一定的时间找到某元素才退出等待
示例代码,常用来判断条件,例如视频播放完显示video-finish改变状态
# -*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver_path = r"D:\python3.7\Chromedriver\chromedriver.exe"
driver = webdriver.Chrome(executable_path=driver_path)
driver.get('https://www.douban.com/')
# driver.implicitly_wait(5) # 隐示等待
element = WebDriverWait(driver,10).until(
EC.presence_of_all_elements_located((By.ID,'anony-movie'))
) # 显示等待,10是10秒
print(element)
有时候我们需要打开多个窗口,但是driver只能定位在其中一个,这个时候我们需要切换句柄
from selenium import webdriver
driver_path = r"D:\python3.7\Chromedriver\chromedriver.exe"
driver = webdriver.Chrome(executable_path=driver_path)
driver.get('https://www.douban.com/')
driver.execute_script("window.open('https://www.baidu.com/')") # js调用打开一个新窗口
print(driver.window_handles) # 打印当前存在的句柄
driver.switch_to.window(driver.window_handles[1]) # 切换到第二个句柄
print(driver.current_url)
driver.switch_to.window(driver.window_handles[0])
print(driver.current_url)
输出
['CDwindow-E407C313CAD4005AA7220D841F3C61D3', 'CDwindow-DF82C60FD10D81FAF402CFF53717B7CC']
https://www.baidu.com/?tn=78040160_15_pg&ch=13
https://www.douban.com/