1、contains 模糊匹配文本
2、contains 模糊匹配元素属性
Contains() 总结
定位其它id,name等属性方式跟class一样: //*[contains(@xxx, "index")]
除了text文本属性定位不一样 , //*[contains(text(), "文本")] ;
比如a标签 <a class="index-font" href="/">回到首页</a>
# -*- coding:utf-8 -*- # Author: Sky # Email: 2780619724@qq.com # Time: 2021/8/20 0:48 # Project: day01 # Module: ch_01.py # Environment: Python3.8.6 , Selenium3 环境 ( 3.141.0 版本) # Environment: Chrome ( 92.0.4515.131, 正式版本) + chromedriver(92.0.4515.107版本) from selenium import webdriver import time driver = webdriver.Chrome() driver.get("http://49.235.92.12:8200/users/login/") driver.maximize_window() time.sleep(3) # link time.sleep(2) driver.find_element_by_link_text("回到首页").click() driver.back() driver.find_element_by_partial_link_text("忘记密码").click() driver.back() # xpath text() 文本定位 time.sleep(2) driver.find_element_by_xpath('//*[text()="[意见反馈]"]').click() driver.back() # contains() 匹配文本 time.sleep(2) driver.find_element_by_xpath('//*[contains(text(), "回到首页")]').click() driver.back() # contains() 部分匹配 time.sleep(2) driver.find_element_by_xpath('//*[contains(text(), "到首页")]').click() driver.back() # class属性 # driver.find_element_by_class_name("index-font").click() # xpath 定位class # driver.find_element_by_xpath('//*[@class="index-font"]').click() # contains() 匹配 # driver.find_element_by_xpath('//*[contains(@class, "index-font")]').click() # contains() 部分匹配 # driver.find_element_by_xpath('//*[contains(@class, "index-")]').click() time.sleep(3) driver.quit()