1. 程式人生 > >selenium 定位元素方式大全

selenium 定位元素方式大全

tar aid input .com element () int path ive

starts-with 顧名思義,匹配一個屬性開始位置的關鍵字

contains 匹配一個屬性值中包含的字符串

text() 匹配的是顯示文本信息,此處也可以用來做定位用

eg

//input[starts-with(@name,‘name1‘)] 查找name屬性中開始位置包含‘name1‘關鍵字的頁面元素

//input[contains(@name,‘na‘)] 查找name屬性中包含na關鍵字的頁面元素

<a href="http://www.baidu.com">百度搜索</a>

xpath寫法為 //a[text()=‘百度搜索‘]

或者 //a[contains(text(),"百度搜索")]

把看不到的元素拉到能看到的地方

driver.find_element_by_xpath("//div[contains(text(), "提交")]").location_once_scrolled_into_view

selenium 定位元素方式大全