python自动化测试——元素定位

<input id="qcreatetask" class="placeholder-italic" placeholder="提交">

 

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.***.cn")

#通过ID定位
driver.find_element(By.ID,"qcreatetask")

#通过CSS定位
driver.find_element(By.CSS_SELECTOR,"#qcreatetask")

#通过CSS属性定位
driver.find_element(By.CSS_SELECTOR,"#qcreatetask[class='placeholder-italic']")

#CSS定位,属性值由多个空格隔开,匹配其中一个值的方法
driver.find_element(By.CSS_SELECTOR,"#qcreatetask[class~='placeholder-italic']")

#CSS定位,匹配属性值为字符串开头的方法
driver.find_element(By.CSS_SELECTOR,"#qcreatetask[class^='plac']")

#CSS定位,匹配属性值字符串结尾的方法
driver.find_element(By.CSS_SELECTOR,"#qcreatetask[class$='italic']")

#CSS定位,匹配被-分割的属性值的方法
driver.find_element(By.CSS_SELECTOR,"#qcreatetask[class|='placeholder']")

#通过XPATH属性定位
driver.find_element(By.XPATH,"//input[@class='placeholder-italic']")

#XPATH定位,字符串开头starts-with() driver.find_element(By.XPATH,"//input[starts-with(@class,'pla')]") #XPATH定位,字符串结尾ends-with() driver.find_element(By.XPATH,"//input[ends-with(@class,'italic')]") #XPATH定位,包含某字符串contains() driver.find_element(By.XPATH,"//input[contains(@class,'holder')]") driver.quit()

 

 

posted @ 2019-02-15 15:56  OTAKU_nicole  阅读(697)  评论(0编辑  收藏  举报