从零开始学UI自动化(含代码)持续更新,建议收藏!"/>
吐血整理 python 从零开始学UI自动化(含代码)持续更新,建议收藏!
第二章
--------------WebDrriver元素定位------------------
白开水笔记,内涵代码可跟着敲一敲,ui自动化必须会的定位
2.0 WebDriver概述
只支持jdk1.8及以上
通过ms官方的webdriver支持Edge浏览器
通过mozilla官方的geckodriver来支持火狐
2.1 安装selenium
安装命令: pip install selenium
查看版本:pip show selenium
dos 退出某个环境:quit()
查看安装路径:where python
重要!!安装火狐浏览器驱动
.31.0
解压后放到py安装目录下
2.2 WebDriver提供了八中定位元素方式,并提供对应的方法
这个是老方法不用了,仅供了解
1* id find_element_by_id("id属性值")
2* name find_element_by_name("name属性值")
3 link text find_element_by_link_text(“连接文本”)
4 partial link text find_element_by_partial_link_text(“部分连接文本”)
5 class name find_element_by_class_name(“类名”)
6 tag name find_element_by_tag_name(“标记名称”)
7* xpath find_element_by_xpath(“xpath表达式”)
8 css selector find_element_by_css_selector(“css表达式”)
新方法统一调用find_element()方法,通过类by来声明定位方法,并传入相应的定位参数
示例:
find_element(定位类型,定位具体信息)
需要导入类by
from selenium.webdribermon.by import By
1* id find_element(By.ID,"id属性值")
2* name find_element(By.NAME,"name属性值")
3 link text find_element(By.LINK_TEXT,“连接文本”)
4 partial link text find_element(By.PARTIAL_LINK_TEXT,“部分连接文本”)
5 class name find_elemente(By.LINK_TEXT,“类名”)
6 tag name find_element(By.TAG_NAME,“标记名称”)
7* xpath find_element(By.XPATH,“xpath表达式”)
8 css selector find_element(By.CSS_SELECTOR,“css表达式”)
2.3 ID定位 核心知识点
from selenium import webdriver # 导入webdriver
from selenium.webdrivermon.by import By #导入By
from time import sleep #导入time
# 对火狐实例化
d = webdriver.Firefox()
# 定位元素
myname = d.find_element(By.ID,"username")
更多推荐
吐血整理 python 从零开始学UI自动化(含代码)持续更新,建议收藏!
发布评论