元素的定位·XPATH

from selenium import webdriver
import time
driver = webdriver.Chrome() # 指定webdriver的位置
driver.get('https://www.baidu.com') # 打开指定的网址
driver.maximize_window()
time.sleep(3)
# xpath 定位
# 1、绝对路径,在同一层下有相同标签的时候可以通过下标进行选择 ,下标从1开始
# driver.find_element_by_xpath('/html/body/div/div/div[3]/a[3]').click()
# 2、相对路径,必须是//
# driver.find_element_by_xpath('//div[3]/a[3]').click()
# 3、通过元素索引 下标从1开始
# driver.find_element_by_xpath('//div[3]/a[4]').click()
# 4、使用元素属性定位
# 4.1 单属性定位
# driver.find_element_by_xpath('//input[@maxlength="255"]').send_keys('单属性定位')
#4.2 多属性定位 and or
# driver.find_element_by_xpath('//input[@maxlength="255" and @autocomplete="off"]').send_keys('多属性定位')
# # driver.find_element_by_xpath('//input[@maxlength="255" or @1="off"]').send_keys('多属性定位')
# 4.3支持通配符
# driver.find_element_by_xpath('//*[@*="255"]').send_keys('通配符')
# 5、使用部分属性值定位
# 5.1 以什么开头starts-with()
# driver.find_element_by_xpath('//a[starts-with(@href,"http://news")]').click()
# 5.2 以什么结尾substring(),下标从1开始
# driver.find_element_by_xpath('//a[substring(@href,13)="hao123.com"]').click()
# 5.3属性值包含contains()
driver.find_element_by_xpath('//a[contains(@href,"hao123.com")]').click()

#6 支持元素文本定位
# driver.find_element_by_xpath('//a[text()="hao123"]').send_keys('xpath文本定位')
# driver.find_element_by_xpath('//a[contains(text(), "hao1")]').click() # 部分匹配

time.sleep(3)
driver.quit() # 关闭整个页面
posted @ 2020-12-14 00:46  jasonchenYT  阅读(45)  评论(0编辑  收藏  举报