python+selenium3--Xpath语法定位元素
python+selenium3--Xpath语法定位元素
通过id定位
语法//*[@id='xxx']
from selenium import webdriver import time driver = webdriver.Firefox() driver.get('http://www.baidu.com') time.sleep(3) #<input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd"/> #通过ID定位 driver.find_element_by_xpath("//*[@id='kw']").send_keys('python')
通过class定位
语法//*[@class='xxx']
from selenium import webdriver import time driver = webdriver.Firefox() driver.get('http://www.baidu.com') time.sleep(3) #<input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd"/> #通过class定位 driver.find_element_by_xpath("//*[@class='s_ipt']").send_keys('python')
通过name定位
语法//*[@name='xxx']
from selenium import webdriver import time driver = webdriver.Firefox() driver.get('http://www.baidu.com') time.sleep(3) #<input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd"/> #通过name定位 driver.find_element_by_xpath("//*[@name='wd']").send_keys('selenium')
通过标签tag定位
语法//xxx[@id='xxx'] @后边可以是id/calss/name或其他属性
from selenium import webdriver import time driver = webdriver.Firefox() driver.get('http://www.baidu.com') time.sleep(3) #<input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd"/> #通过tag定位 driver.find_element_by_xpath(".//input[@id='kw']").send_keys('python')
通过组合定位
from selenium import webdriver import time driver = webdriver.Firefox() driver.get('http://www.baidu.com') time.sleep(3) #<input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd"/> #通过组合定位 driver.find_element_by_xpath("//*[@autocomplete='off' and @name='wd']").send_keys('python')
通过文本定位
语法.//*[text()='文本'] 注:text之前没有@
from selenium import webdriver import time driver = webdriver.Firefox() driver.get("http://www.baidu.com") time.sleep(3) #<a class="mnav c-font-normal c-color-t" target="_blank" href="https://www.hao123.com">hao123</a> #通过文本定位 driver.find_element_by_xpath(".//*[text()='hao123']").click()
通过层级关系定位
from selenium import webdriver from time import sleep driver = webdriver.Firefox() driver.get(r'E:\MyPro_han\WebTest01\select11\selectTest.html') driver.implicitly_wait(10) sleep(2) driver.find_element_by_xpath(".//*[@id='nr']/option[3]").click() sleep(2) driver.close()
同一个父级多个子级
from selenium import webdriver from time import sleep driver = webdriver.Firefox() driver.get("http://www.baidu.com") driver.implicitly_wait(10) sleep(2) """ <div id="s-top-left" class="s-top-left s-isindex-wrap"> <a class="mnav c-font-normal c-color-t" target="_blank" href="http://news.baidu.com">新闻</a> <a class="mnav c-font-normal c-color-t" target="_blank" href="https://www.hao123.com">hao123</a> <a class="mnav c-font-normal c-color-t" target="_blank" href="http://map.baidu.com">地图</a> <a class="mnav c-font-normal c-color-t" target="_blank" href="https://haokan.baidu.com/?sfrom=baidu-top">视频</a> <a class="mnav c-font-normal c-color-t" target="_blank" href="http://tieba.baidu.com">贴吧</a> <a class="mnav c-font-normal c-color-t" target="_blank" href="http://xueshu.baidu.com">学术</a> """ ### 下标从1开始 driver.find_element_by_xpath(".//*[@id='s-top-left']/a[2]").click() sleep(2) #或者是 # driver.find_element_by_xpath(".//*[@id='s-top-left']/a[@target='_blank'][2]").click() # sleep(2) driver.close()
模糊匹配
from selenium import webdriver from time import sleep driver = webdriver.Firefox() driver.get('http://www.baidu.com') driver.implicitly_wait(10) #contains 模糊匹配 #匹配文本 driver.find_element_by_xpath(".//*[contains(text(), '新')]").click() #匹配某个属性 driver.find_element_by_xpath(".//*[contains(@id, 'kw')]").send_keys("123") #匹配以xx开头 driver.find_element_by_xpath(".//*[starts-with(text(), ''hao)]").click() #匹配以xx结尾 driver.find_element_by_xpath(".//*[ends-with(text(), '新闻')]").click()