selenium八大元素定位分类+查找元素
''
'''
8种定位方式是不分语言的 - 定位的是html页面
定位:在整个html当中,根据元素的特色,找到它!
八种定位方式有2类:
1.根据元素的一个特征来定位,有6种方式
1.1 id: 元素的id属性。- 唯一。
1.2 class: 元素的class属性
1.3 tag: 元素的标签名
1.4 name: 元素的name属性
针对a元素:
1.5 link_text: 针对a元素的文本内容 - 完全匹配
1.6 partial_link_text: 针对a元素的文本内容 - 包含,就是模糊匹配
2.根据组合元素的多种特征/关系来定位,有2种方式 - 被称为万能定位。
2.1 xpath:
2.2 css selector: css选择器
定位的时候:
1、确认你要找的元素是谁
2、通过F12查看你的元素特征
'''
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys("selenium webdriver")
sleep(7)
driver.close()
driver.quit()
selenium介绍
selenium - 工具包
ide: 录制工具。
webdriver: 第三方库。结合代码来使用。python,java,c
grid: 分布式。
安装第三方库:
pip install -U selenium
web自动化环境:
人 --- 浏览器 - 打开页面,点点点
代码 --- 浏览器 - 打开页面,点点点
代码<--->中间物种(浏览器驱动程序)<--->浏览器(ie、firefox、google)
执行步骤:
1.指令(客户端):执行指令(地址+端口 - 服务端)
2.打开一个浏览器:每个浏览器都有一个驱动程序
3.访问百度首页
4.输入用户名
5.输入密码
6.点击搜索
浏览器驱动:(ie、firefox、google)---(ps:驱动程序要跟浏览器版本匹配)
1.Ie的驱动程序:ieserverdriver
Ie驱动下载地址是:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
2.firefox的驱动程序: geckodriver
firefox驱动下载地址是:https://github.com/mozilla/geckodriver/releases
3.google的驱动程序:chromedriver
Chrome官方地址: https://sites.google.com/a/chromium.org/chromedriver/downloads .
国内镜像地址:http://npm.taobao.org/mirrors/chromedriver/
4.Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10/
selenium+python环境安装博客地址:
https://www.cnblogs.com/Simple-Small/p/10065674.html