Selenium不同的定位方式

2021-08-20 17:21:42 字數 1503 閱讀 9746

driver.findelement(by.id("id值"));

driver.findelement(by.name("name值"));

driver.findelement(by.linktext("鏈結的全部文字內容"));

driver.findelement(by.partiallinktext("鏈結的部分文字內容"));

driver.findelement(by.xpath("xpath定位表示式"));

/* * 基本xpath表示式       /html/body/div/input[@value="查詢"]

* 使用相對路徑定位元素  //input[@value="查詢"]

* 使用索引進行定位         //input[2]

* 使用頁面元素屬性進行定位  //img[@alt='div1-img1']

*                 //img[@href=""]

*                 //div[@name='div2']/input[@name='div2input']

*                 //div[@id='div1']/a[@href='']

*                 //input[@type='button']

*模糊定位

*查詢alt屬性開始位置包含div關鍵字的頁面元素  //img[start-with(@alt,'div')]

*查詢alt屬性包含gl關鍵字的頁面元素 //img[contains(@alt,'gl')]

* * 使用xpath軸進行元素定位

*  //img[@alt='div2-img2']parent::div 選擇當前節點的上層父節點

*  //div[@id='div1']/child::img  選擇當前節點的下層子節點

*  //img[@alt='div2-img2']/ancestor::div 選擇當前節點的所有上層節點

*  //div[@name='div2']/descendant:img 選擇當前節點的所有下層節點

*  //div[@id='div']/following::img 選擇當前節點之後跟隨的所有節點

*  //a[@href=""]/follpwing-sibling:input 選擇當前節點的所有平級節點

*  //img[@alt='div2-img2']/preceding::div 選擇當前節點前面的所有節點

*  //img[@alt='div2-img2']/preceding-sibling::[a1]選擇當前節點前面的所有同級節點

*  *  使用頁面元素文字定位元素

* */

driver.findelement(by.cssselector("css定位表示式"));

driver.findelement(by.classname("頁面元素的class屬性"));

driver.findelement(by.tagname("頁面中html標籤名稱"));

selenium 定位方式說明

1.通過id定位 driver.find element by id sb form q send keys 51testing 2.通過name 定位 driver.find element by name address mobile send keys 15036121234 3.通過clas...

selenium 常用定位方式

from selenium import webdriver option webdriver.chromeoptions option.add experimental option useautomationextension false option.add experimental opti...

selenium 元素的定位方式

find element by id 直接呼叫型 from selenium.webdriver.common.by import by find element by.id,value 使用by型別 需要匯入by from selenium import webdriver import time...