一、介绍
'''
selenium的介绍
-- 是一个用于Web应用程序测试的工具
-- 直接运行在浏览器中,就像真正的用户在操作一样
-- 支持各种驱动(driver)
-- 支持无界面浏览器操作
-- 模拟浏览器功能,自动执行JS代码
最重要的特点:
-- 1. 可以在浏览器总直接执行JS代码
-- 2. 可以模仿人在浏览器进行一系列的操作
为什么使用?
-- 有时候模拟浏览器去获取数据,会获取不到
-- 此时就需要有驱动帮助我们,用一个驱动去操作另一个驱动
'''
二、安装
'''
安装selenium:【仅针对Chrom浏览器】
1. 查看当前Chrom浏览器的版本:浏览器右上角 --> 帮助 --> 查看版本
2. http://chromedriver.storage.googleapis.com/index.html下载浏览器对应版本的驱动
3. 下载后是一个zip压缩包,解压得到一个 *.exe文件
4. 将 *.exe文件放到与python文件同级的目录下
5. Python安装selenium库:pip install selenium
6. 导包:from selenium import webdriver
'''
三、简单使用
from selenium import webdriver
path = 'chromedriver.exe'
browser = webdriver.Chrome(path)
url = 'https://www.baidu.com'
browser.get(url=url)
content = browser.page_source
四、元素定位
from selenium import webdriver
path = 'chromedriver.exe'
browser = webdriver.Chrome(path)
url = 'https://www.baidu.com'
browser.get(url=url)
a = browser.find_element_by_id('su')
print(type(a))
a = browser.find_element_by_xpath('//div')
print(a)
a = browser.find_elements_by_xpath('//div')
print(type(a))
a = browser.find_element_by_css_selector('#su')
print(a)
a = browser.find_elements_by_css_selector('#su')
print(a)
a = browser.find_element_by_link_text('hao123')
print(a)
五、获取WebElement对象的属性
from selenium import webdriver
path = 'chromedriver.exe'
browser = webdriver.Chrome(path)
url = 'https://www.baidu.com'
browser.get(url=url)
input = browser.find_element_by_id('kw')
print(input)
print(input.get_attribute('class'))
print(input.get_attribute('type'))
print(input.get_attribute('name'))
print(input.tag_name)
print(input.text)
六、程序与浏览器的自动交互
from selenium import webdriver
import time
path = 'chromedriver.exe'
browser = webdriver.Chrome(path)
url = 'https://www.baidu.com'
browser.get(url=url)
t = 3
browser.get(url)
time.sleep(t)
input = browser.find_element_by_id('kw')
input.send_keys('周杰伦')
time.sleep(t)
button = browser.find_element_by_id('su')
button.click()
time.sleep(t)
browser.save_screenshot('baidu.jpg')
time.sleep(t)
js= 'scroll(0,100000)'
browser.execute_script(js)
time.sleep(t)
nextPage = browser.find_element_by_xpath('//a[@class="n"]')
nextPage.click()
time.sleep(t)
browser.back()
time.sleep(t)
browser.forward()
time.sleep(t)
browser.quit()
七、无界面操作
1. 无界面的介绍
'''
selenium的的效率较低,因为打开浏览器会,加载页面,会消耗时间
Phantomjs:【不推荐】
一种浏览器无界面模式,可以不打开 UI 界面的情况下使用 Chrom浏览器
支持JS执行
不进行css渲染,gui渲染,运行效率高
Chrom handless【官方的东西,推荐】
一种浏览器无界面模式,可以不打开 UI 界面的情况下使用 Chrom浏览器
支持JS执行
是 Google 针对 Chrom浏览器59版本新增的一种模式
效果一样,但是效率更高
'''
2. Chrom handless简单使用
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
path = 'C:\Program Files\Google\Chrome\Application\chrome.exe'
chrome_options.binary_location = path
browser = webdriver.Chrome(chrome_options=chrome_options)
url = 'https://www.baidu.com'
browser.get(url)
browser.save_screenshot('baidu.jpg')
3. 简单的封装
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def getBrowser():
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
path = 'C:\Program Files\Google\Chrome\Application\chrome.exe'
chrome_options.binary_location = path
return webdriver.Chrome(chrome_options=chrome_options)
browser = getBrowser()
url = 'https://www.baidu.com'
browser.get(url)
browser.save_screenshot('baidu.jpg')