Python腳本Selenium及頁(yè)面Web元素定位詳解
Selenium特點(diǎn)
開(kāi)源,免費(fèi)
多瀏覽器支持:firefox、chrome、IE
多平臺(tái)支持:linux 、windows、MAC
多語(yǔ)言支持:java、python、ruby、php、C#、
對(duì)web頁(yè)面有良好的支持
簡(jiǎn)單(API 簡(jiǎn)單)、靈活(用開(kāi)發(fā)語(yǔ)言驅(qū)動(dòng))
支持分布式測(cè)試用例執(zhí)行一、
八種定位方式
1、driver.find_element_by_xpath(value)
可以使用自帶的copy xpath 進(jìn)行定位
不推薦使用這種方法進(jìn)行元素定位,后續(xù)開(kāi)發(fā)修改代碼路徑發(fā)生變化就需要重新進(jìn)行定位
// 使用xpath進(jìn)行定位 from selenium import webdriver driver = webdriver.Chrome() //打開(kāi)谷歌 driver.get("http://www.baidu.com") //打開(kāi)百度鏈接 //通過(guò)id定位到輸入框的位置,send_keys('') 往輸入框填寫(xiě)內(nèi)容 driver.find_element_by_name("wd").send_keys("Selenium八大元素定位") //通過(guò)id定位到按鈕的位置并進(jìn)行點(diǎn)擊,click() 點(diǎn)擊操作 driver.find_element_by_id("su").click() //通過(guò)xpath定位獲取元素位置 driver.find_element_by_xpath('//*[@id="2"]/h3/a').click()
利用元素屬性進(jìn)行xpath定位
// 利用元素屬性進(jìn)行xpath定位 from selenium import webdriver driver = webdriver.Chrome() //打開(kāi)谷歌 driver.get("http://www.baidu.com") //打開(kāi)百度鏈接 //通過(guò)id定位到輸入框的位置,send_keys('') 往輸入框填寫(xiě)內(nèi)容 driver.find_element_by_name("wd").send_keys("Selenium八大元素定位") //通過(guò)id定位到按鈕的位置并進(jìn)行點(diǎn)擊,click() 點(diǎn)擊操作 driver.find_element_by_id("su").click() //通過(guò)元素屬性進(jìn)行xpath定位 元素的值需要是唯一的 driver.find_element_by_xpath('//a[@ rel="external nofollow" ]').click()
2、driver.find_element_by_css_selector(value)
// 搜索百度使用f12定位到輸入框的位置 <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
from selenium import webdriver driver = webdriver.Chrome() //打開(kāi)谷歌 driver.get("http://www.baidu.com") //打開(kāi)百度鏈接 //標(biāo)簽名及屬性(含屬性值)組合定位,方式有很多不一一舉例 driver.find_element_by_css_selector('input[name="wd"]').send_keys("Selenium八大元素定位")
3、driver.find_element_by_id(value)
// 搜索百度使用f12定位到按鈕的位置 <input type="submit" id="su" value="百度一下" class="bg s_btn">
// 使用id 定位到輸入框的位置 from selenium import webdriver driver = webdriver.Chrome() //打開(kāi)谷歌 driver.get("http://www.baidu.com") //打開(kāi)百度鏈接 //通過(guò)id定位到按鈕的位置并進(jìn)行點(diǎn)擊,click() 點(diǎn)擊操作 driver.find_element_by_id("su").click()
4、driver.find_element_by_name(value)
// 搜索百度使用f12定位到輸入框的位置 <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
// 使用name 定位到輸入框的位置 from selenium import webdriver driver = webdriver.Chrome() //打開(kāi)谷歌 driver.get("http://www.baidu.com") //打開(kāi)百度鏈接 //通過(guò)id定位到輸入框的位置,send_keys('') 往輸入框填寫(xiě)內(nèi)容 driver.find_element_by_name("wd").send_keys("Selenium八大元素定位")
5、driver.find_element_by_class_name(value)
// 搜索百度使用f12定位到輸入框的位置 <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
// 使用name 定位到輸入框的位置 from selenium import webdriver driver = webdriver.Chrome() //打開(kāi)谷歌 driver.get("http://www.baidu.com") //打開(kāi)百度鏈接 driver.find_element_by_class_name("s_ipt").send_keys("Selenium八大元素定位")
6、driver.find_element_by_tag_name(value)
// 使用標(biāo)簽名進(jìn)行定位 from selenium import webdriver driver = webdriver.Chrome() //打開(kāi)谷歌 driver.get("http://www.baidu.com") //打開(kāi)百度鏈接 driver.find_element_by_tag_name("input")//通過(guò)標(biāo)簽名去定位,不推薦重復(fù)率很高
7、driver.find_element_by_link_text(value)
// 搜索百度使用f12定位到按鈕的位置 <a rel="external nofollow" rel="external nofollow" target="_blank" class="mnav c-font-normal c-color-t">新聞</a>
//此定位方式主要是對(duì)超鏈接進(jìn)行定位,填寫(xiě)的內(nèi)容是完整的超鏈接文字 from selenium import webdriver driver = webdriver.Chrome() //打開(kāi)谷歌 driver.get("http://www.baidu.com") //打開(kāi)百度鏈接 //此定位方式主要是對(duì)超鏈接進(jìn)行定位,也就是html中的<a>標(biāo)簽,括號(hào)中填寫(xiě)的值是完整的超鏈接文字 driver.find_element_by_link_text("新聞").click()
8、driver.find_element_by_partial_link_text(value)
// 搜索百度使用f12定位到按鈕的位置 <a rel="external nofollow" rel="external nofollow" target="_blank" class="mnav c-font-normal c-color-t">新聞</a>
//此定位方式主要是對(duì)超鏈接進(jìn)行定位,填寫(xiě)的內(nèi)容是部分的超鏈接文字 from selenium import webdriver driver = webdriver.Chrome() //打開(kāi)谷歌 driver.get("http://www.baidu.com") //打開(kāi)百度鏈接 //此定位方式主要是對(duì)超鏈接進(jìn)行定位,也就是html中的<a>標(biāo)簽,括號(hào)中填寫(xiě)的值是部分的超鏈接文字 driver.find_element_by_partial_link_text("新").click()
以上就是Python腳本Selenium及頁(yè)面Web元素定位詳解的詳細(xì)內(nèi)容,更多關(guān)于腳本Selenium頁(yè)面Web元素定位的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python+Selenium定位不到元素常見(jiàn)原因及解決辦法(報(bào):NoSuchElementException)
- python中通過(guò)selenium簡(jiǎn)單操作及元素定位知識(shí)點(diǎn)總結(jié)
- 處理Selenium3+python3定位鼠標(biāo)懸停才顯示的元素
- Python selenium根據(jù)class定位頁(yè)面元素的方法
- Python2 Selenium元素定位的實(shí)現(xiàn)(8種)
- python+selenium 定位到元素,無(wú)法點(diǎn)擊的解決方法
- selenium+python自動(dòng)化測(cè)試之頁(yè)面元素定位
- Python?selenium?八種定位元素的方式
相關(guān)文章
Python request使用方法及問(wèn)題總結(jié)
這篇文章主要介紹了Python request使用方法及問(wèn)題總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Python 內(nèi)置函數(shù)globals()和locals()對(duì)比詳解
這篇文章主要介紹了Python globals()和locals()對(duì)比詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12pandas 中對(duì)特征進(jìn)行硬編碼和onehot編碼的實(shí)現(xiàn)
今天小編就為大家分享一篇pandas 中對(duì)特征進(jìn)行硬編碼和onehot編碼的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12matplotlib之Pyplot模塊繪制三維散點(diǎn)圖使用顏色表示數(shù)值大小
在撰寫(xiě)論文時(shí)常常會(huì)用到matplotlib來(lái)繪制三維散點(diǎn)圖,下面這篇文章主要給大家介紹了關(guān)于matplotlib之Pyplot模塊繪制三維散點(diǎn)圖使用顏色表示數(shù)值大小的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08Python實(shí)現(xiàn)簡(jiǎn)單的猜單詞
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)簡(jiǎn)單的猜單詞,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06