济南手机网站,网店设计公司,网站专业术语中seo意思是,众安保险网站目录
1、selenium介绍
2、selenium的作用#xff1a; 3、配置浏览器驱动环境及selenium安装
4、selenium基本语法
4.1、selenium元素的定位
4.2、selenium元素的信息
4.3、selenium元素的交互
5、Phantomjs介绍
6、chrome handless模式 1、selenium介绍 #xff08;1…目录
1、selenium介绍
2、selenium的作用 3、配置浏览器驱动环境及selenium安装
4、selenium基本语法
4.1、selenium元素的定位
4.2、selenium元素的信息
4.3、selenium元素的交互
5、Phantomjs介绍
6、chrome handless模式 1、selenium介绍 1selenium是一个用于web应用程序测试的工具。 2selenium测试直接运行在浏览器中就像真正的用户在操作一样。 3支持通过各种driverFirfoxDriverIternetExplorerDriverOperaDriverChromeDriver驱动真是浏览器完成测试。 4selenium也是支持无界面浏览器操作的。 2、selenium的作用 1可以模拟用户在浏览器中的各种操作如点击按钮、输入文本、提交表单等用于对web应用程序进行功能测试回归测试等。 2可以用于自动化一些重复性的网页操作任务如批量上传文件、定时执行任务提高工作效率。 3爬取一些我们无法获取的数据比如京东上的限时秒杀数据等等。 3、配置浏览器驱动环境及selenium安装
1操作chrome浏览器下载 浏览器的驱动要下载和浏览器配套的版本。将下载的浏览器驱动放到python的安装目录下并配置系统环境变量。 安装地址官网
2selenium的安装 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple selenium 3测试浏览器驱动是否正常
from selenium import webdriver
# 创建浏览器操作对象
driver webdriver.Chrome()
driver.get(http://www.baidu.com)
input()
4、selenium基本语法
4.1、selenium元素的定位
元素定位自动化要做的就是模拟鼠标和键盘来操作这些元素点击、输入等等。操作这些元素首先要找到他们webdriver提供很多元素定位的方法。
1id可以唯一定位到一个元素以百度的百度一下四个字为例
button driver.find_element(By.ID,su)
2name要确保是全局唯一的以百度的文本搜索框为例
button driver.find_element(By.NAME,wd)
3xpath全局唯一
button driver.find_element(By.XPATH,//input[idsu])
4tag name标签即标签名字
button driver.find_element(By.TAG_NAME,input)
5css selector元素样式通过bs4的语法来获取对象
button driver.find_element(By.CSS_SELECTOR, #su)
6link text获取链接文本
button driver.find_element(By.LINK_TEXT, 新闻)
4.2、selenium元素的信息
1通过get_attribute来获取class的属性值
input driver.find_element(By.ID, su)
print(input.get_attribute(class))
2通过text获取元素文本只能获取两个标签之间的数据哦
a driver.find_element(By.LINK_TEXT, 新闻)
print(a.text)
3通过tag_name获取标签名
input driver.find_element(By.ID, su)
print(input.tag_name)
4.3、selenium元素的交互 1click点击对象以点击百度一下按钮为例
button driver.find_element(By.ID,su).click()
2send_keys在对象上模拟按键输入搜索框中输入周杰伦
input driver.find_element(By.ID,kw).send_keys(周杰伦)
3滑到底部
js_bottom document.documentElement.scrollTop10000
driver.execute_script(js_bottom)
4回退选项
driver.back()
5返回上一选项
driver.forward()
案例在百度搜索框中搜索周杰伦翻到最后一页打开下一页回退选项在返回上一选项。最后退出浏览器
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver webdriver.Chrome()
url https://www.baidu.com
driver.get(url)
time.sleep(2)
input driver.find_element(By.ID,kw).send_keys(周杰伦)
time.sleep(2)
button driver.find_element(By.ID,su).click()
time.sleep(2)
js_bottom document.documentElement.scrollTop10000
driver.execute_script(js_bottom)
time.sleep(2)
# 获取下一页的按钮
next_button driver.find_element(By.XPATH,//a[classn]).click()
time.sleep(2)
# 回退回到上一页
driver.back()
time.sleep(2)
# 返回上一选项
driver.forward()
time.sleep(5)
driver.quit() 5、Phantomjs介绍 1是一个无界面的浏览器 2支持页面元素查找js的执行等 3由于不进行css和gui渲染运行效率要比真实的浏览器要快很多 但是Phantomjs这个公司已经黄了维护者已经辞职并停止维护了因此这里就不讲解了。
6、chrome handless模式
chrome-handless模式Google针对chrome浏览器59版本新增加的一种模式可以在不打开UI界面的情况下使用chrome浏览器所以运行效果与chrome保持一致。 系统要求 chrome Unix/Linux 系统需要 chrome 59 windows 系统需要 chrome 60 python版本 3.6 selenium版本 3.4.* chromedriver版本 2.31 from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
chrome_options Options()
chrome_options.add_argument(--headless)
chrome_options.add_argument(--disable-gpu)
driver webdriver.Chrome(optionschrome_options)
url https://www.baidu.com
driver.get(url)
driver.save_screenshot(screenshot.png) 但这样每次都需要配置的话会比较麻烦我们只要封装到函数里面那么每次用只需要调用函数
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def share_browser():chrome_options Options()chrome_options.add_argument(--headless)chrome_options.add_argument(--disable-gpu)driver webdriver.Chrome(optionschrome_options)return driver
driver share_browser()