学习笔记~
from selenium import webdriver
from selenium.webdriver.common.by import *
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
#
# # ID定位元素
# el1 = driver.find_element(By.ID,'kw')
# print(el1)
#
# # name定位元素
# el2 = driver.find_element(By.NAME,'wd')
# print(el2)
#
# # 通过class定位 不唯一
# el3 = driver.find_element(By.CLASS_NAME,'s_ipt')
# print(el3)
#
# # 通过标签名定位 不唯一
# el4 = driver.find_element(By.TAG_NAME,'form')
# print(el4)
#
# # 通过链接文本定位,只能定位a标签
# el5 = driver.find_element(By.LINK_TEXT, '新闻')
# print(el5)
#
# # 部分链接文本定位
# el6 = driver.find_element(By.PARTIAL_LINK_TEXT, 'hao')
# print(el6)
# ========css选择器定位===========
# # css结合基本属性定位 # 代表id,id=id, . 代表class=class
#
# # css+id定位
# el7 = driver.find_element(By.CSS_SELECTOR, '#kw')
# print(el7)
#
# # css+class定位
# el8 = driver.find_element(By.CSS_SELECTOR, '.s_ipt')
# print(el8)
#
# # css+标签名定位
# el9 = driver.find_element(By.CSS_SELECTOR, 'form')
# print(el9)
# # =========css结合其他属性定位==============
# el10 = driver.find_element(By.CSS_SELECTOR,"[name='wd'][id='kw']")
# print(el10)
# # =========css标签结合其他属性定位===========
# el11 = driver.find_element(By.CSS_SELECTOR,"input#kw") # 标签+ID
# print(el11)
# el12 = driver.find_element(By.CSS_SELECTOR,"input[name='wd']") # 标签+name属性
# print(el12)
# el13 = driver.find_element(By.CSS_SELECTOR,"input[name='wd'][id='kw']") # 标签+name属性+ID
# print(el13)
# el14 = driver.find_element(By.CSS_SELECTOR,"input.s_ipt") # 标签+class
# print(el14)
# el15 = driver.find_element(By.CSS_SELECTOR,"input[name='wd'][class='s_ipt']") # 标签+name属性+class
# print(el15)
# el16 = driver.find_element(By.CSS_SELECTOR,"input[name='wd'][class='s_ipt'][id='kw']") # 标签+name属性+class+ID
# print(el16)
# ========css层级定位==========
# el17 = driver.find_element(By.CSS_SELECTOR,"form[id=form]>span>input.s_ipt") # 通过父类 一级一级往下定位
# print(el17)
# # ========css索引定位==========索引是从1开始的.不是0 nth-child() nth-last-child()
# el18 = driver.find_element(By.CSS_SELECTOR,"div[id='s-top-left']>a:nth-child(3)")# 定位百度首页的 '地图'
# print(el18.text) # 输出 地图
# # 倒数定位
# el19 = driver.find_element(By.CSS_SELECTOR,"div[id='s-top-left']>a:nth-last-child(5)")
# print(el18.text) # 输出 地图
# # ============ 模糊匹配 =============
# 属性值由多个空格隔开,匹配其中一个值的方法
# 匹配字符部分 driver.find_element(By.CSS_SELECTOR,"[属性名~='部分属性值 ]")
# el20 = driver.find_element(By.CSS_SELECTOR, "input[class='bg s_btn'")
# print(el20)
#
# # 匹配字符部分 driver.find_element(By.CSS_SELECTOR,"[属性名~='部分属性值 ]")
# el21 = driver.find_element(By.CSS_SELECTOR, "input[class~='s_btn']")
# print(el21)
# 匹配字符串开头 driver.find_element(By.CSS_SELECTOR,"[属性名^='属性值开头]”)
# el22 = driver.find_element(By.CSS_SELECTOR, "input[class^='bg']")
# print(el22)
#
# # 匹配字符串结尾 driver.find_element(By.CSS_SELECTOR,"[属性名$='属性值结尾']")
# el23 = driver.find_element(By.CSS_SELECTOR,"input[class$='n']")
# print(el23)
# # =============通过xpath定位=================
# / 从根节点选取 (取子节点)
# // 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置 (取子孙节点)
# . 选取当前节点。
# .. 选取当前节点的父节点
# @ 选取属性,后面跟属性的KEY,表示要通过哪个属性定位 @key='value'。
# # 1.xpath结合基本属性定位
# el24 = driver.find_element(By.XPATH, ".//input[@id='kw']")
# print(el24)
# el25 = driver.find_element(By.XPATH, ".//input[@id='kw' and @name='wd']")
# print(el25)
# 2.xpath文本定位
# el26 = driver.find_element(By.XPATH, ".//a[text()='新闻']")
# print(el26.text)
# 3.xpath层级定位
# 由上到下的层级关系
# el27 = driver.find_element(By.XPATH,".//form[@id='form']/span/input")
# print(el27)
# 父子元素定位 # parent::span : 向父级定位,父级的标签名为span
# el28 = driver.find_element(By.XPATH,".//input[@id='kw']/parent::span")
# print(el28)
# 哥哥元素定位 # preceding-sibling::span : 向哥哥级定位,哥哥级的标签名为span
# el29 = driver.find_element(By.XPATH, ".//input[@id='kw']/preceding-sibling::span")
# print(el29)
# # 弟弟元素定位 # following-sibling::span : 向弟弟级定位,弟弟级的标签名为span
# el30 = driver.find_element(By.XPATH, ".//input[@id='kw']/following-sibling::span")
# print(el30)
# 4.xpath索引定位
# el31 = driver.find_element(By.XPATH, ".//div[@id='s-top-left']/a[1]") # 获取第一个 同级标签
# print(el31.text)
#
# el32 = driver.find_element(By.XPATH,".//div[@id='s-top-left']/a[last()]") # 获取倒数第一个标签
# print(el32.text)
#
# el33 = driver.find_element(By.XPATH,".//div[@id='s-top-left']/a[last()-1]") # 获取倒数第二个标签
# print(el33.text)
# 5.xpath模糊匹配
# starts-with(): 匹配开头部分字符
el34 = driver.find_element(By.XPATH, ".//input[starts-with(@value,'百度')]")
print(el34)
# # ends-with(): 匹配结尾部分字符 xpath2.0的语法,浏览器是1.0会报错
# el35 = driver.find_element(By.XPATH, ".//input[ends-with(@value,'下')]")
# print(el35)
# contains(): 包含匹配部分字符
el35 = driver.find_element(By.XPATH, ".//input[contains(@value,'一')]")
print(el35)