Python腳本Selenium及頁面Web元素定位詳解
Selenium特點
開源,免費
多瀏覽器支持:firefox、chrome、IE
多平臺支持:linux 、windows、MAC
多語言支持:java、python、ruby、php、C#、
對web頁面有良好的支持
簡單(API 簡單)、靈活(用開發(fā)語言驅(qū)動)
支持分布式測試用例執(zhí)行一、
八種定位方式
1、driver.find_element_by_xpath(value)

可以使用自帶的copy xpath 進行定位
不推薦使用這種方法進行元素定位,后續(xù)開發(fā)修改代碼路徑發(fā)生變化就需要重新進行定位
// 使用xpath進行定位
from selenium import webdriver
driver = webdriver.Chrome() //打開谷歌
driver.get("http://www.baidu.com") //打開百度鏈接
//通過id定位到輸入框的位置,send_keys('') 往輸入框填寫內(nèi)容
driver.find_element_by_name("wd").send_keys("Selenium八大元素定位")
//通過id定位到按鈕的位置并進行點擊,click() 點擊操作
driver.find_element_by_id("su").click()
//通過xpath定位獲取元素位置
driver.find_element_by_xpath('//*[@id="2"]/h3/a').click()
利用元素屬性進行xpath定位
// 利用元素屬性進行xpath定位
from selenium import webdriver
driver = webdriver.Chrome() //打開谷歌
driver.get("http://www.baidu.com") //打開百度鏈接
//通過id定位到輸入框的位置,send_keys('') 往輸入框填寫內(nèi)容
driver.find_element_by_name("wd").send_keys("Selenium八大元素定位")
//通過id定位到按鈕的位置并進行點擊,click() 點擊操作
driver.find_element_by_id("su").click()
//通過元素屬性進行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() //打開谷歌
driver.get("http://www.baidu.com") //打開百度鏈接
//標簽名及屬性(含屬性值)組合定位,方式有很多不一一舉例
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() //打開谷歌
driver.get("http://www.baidu.com") //打開百度鏈接
//通過id定位到按鈕的位置并進行點擊,click() 點擊操作
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() //打開谷歌
driver.get("http://www.baidu.com") //打開百度鏈接
//通過id定位到輸入框的位置,send_keys('') 往輸入框填寫內(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() //打開谷歌
driver.get("http://www.baidu.com") //打開百度鏈接
driver.find_element_by_class_name("s_ipt").send_keys("Selenium八大元素定位")
6、driver.find_element_by_tag_name(value)

// 使用標簽名進行定位
from selenium import webdriver
driver = webdriver.Chrome() //打開谷歌
driver.get("http://www.baidu.com") //打開百度鏈接
driver.find_element_by_tag_name("input")//通過標簽名去定位,不推薦重復率很高
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>
//此定位方式主要是對超鏈接進行定位,填寫的內(nèi)容是完整的超鏈接文字
from selenium import webdriver
driver = webdriver.Chrome() //打開谷歌
driver.get("http://www.baidu.com") //打開百度鏈接
//此定位方式主要是對超鏈接進行定位,也就是html中的<a>標簽,括號中填寫的值是完整的超鏈接文字
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>
//此定位方式主要是對超鏈接進行定位,填寫的內(nèi)容是部分的超鏈接文字
from selenium import webdriver
driver = webdriver.Chrome() //打開谷歌
driver.get("http://www.baidu.com") //打開百度鏈接
//此定位方式主要是對超鏈接進行定位,也就是html中的<a>標簽,括號中填寫的值是部分的超鏈接文字
driver.find_element_by_partial_link_text("新").click()
以上就是Python腳本Selenium及頁面Web元素定位詳解的詳細內(nèi)容,更多關于腳本Selenium頁面Web元素定位的資料請關注腳本之家其它相關文章!
相關文章
放棄 Python 轉(zhuǎn)向 Go語言有人給出了 9 大理由
今年 Stream 團隊的主要編程語言從 Python 轉(zhuǎn)向了 Go。本文解釋了其背后的九大原因以及如何做好這一轉(zhuǎn)換。下面小編給大家分享放棄 Python 轉(zhuǎn)向 Go語言有人給出了 9 大理由,一起看看吧2017-10-10
如何爬取通過ajax加載數(shù)據(jù)的網(wǎng)站
這篇文章主要介紹了如何爬取通過ajax加載數(shù)據(jù)的網(wǎng)站,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08
利用Python第三方庫xlrd讀取Excel中數(shù)據(jù)實例代碼
python操作excel主要用到xlrd和xlwt這兩個庫,即xlrd是讀excel,xlwt是寫excel的庫,下面這篇文章主要給大家介紹了關于利用Python第三方庫xlrd讀取Excel中數(shù)據(jù)的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07
Python提取轉(zhuǎn)移文件夾內(nèi)所有.jpg文件并查看每一幀的方法
今天小編就為大家分享一篇Python提取轉(zhuǎn)移文件夾內(nèi)所有.jpg文件并查看每一幀的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
教你如何用python開發(fā)一款數(shù)字推盤小游戲
這篇文章主要介紹了教你如何用python開發(fā)一款數(shù)字推盤小游戲,文中有非常詳細的代碼示例,喜對歡玩小游戲的或者正在學習python小游戲開發(fā)的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04

