python-web根據(jù)元素屬性進(jìn)行定位的方法
1. 根據(jù)屬性ID值進(jìn)行定位
def test_find_element_by_id(self): # 定位搜索文本框 search_input = self.driver.find_element_by_id("kw") # 輸入關(guān)鍵字 search_input.send_keys("馬云") # 定位搜索按鈕 search_button = self.driver.find_element_by_id("su") # 點(diǎn)擊搜索按鈕 search_button.click() # 喘口氣 time.sleep(2) # 斷言結(jié)果 actual_result = self.driver.page_source expect_result = "馬云" self.assertIn(expect_result, actual_result)
2. 根據(jù)屬性CLASS值進(jìn)行定位
def test_find_element_by_class_name(self): # 定位搜索文本框 search_input = self.driver.find_element_by_class_name("s_ipt") # 輸入關(guān)鍵字 search_input.send_keys("奧巴馬") # 定位搜索按鈕 search_button = self.driver.find_element_by_id("su") # 點(diǎn)擊搜索按鈕 search_button.click() # 喘口氣 time.sleep(2) # 斷言結(jié)果 actual_result = self.driver.page_source expect_result = "奧巴馬" self.assertIn(expect_result, actual_result)
3. 根據(jù)屬性NAME值進(jìn)行定位
def test_find_element_by_name(self): # 定位搜索文本框 search_input = self.driver.find_element_by_name("wd") # 輸入關(guān)鍵字 search_input.send_keys("特朗普") # 定位搜索按鈕 search_button = self.driver.find_element_by_id("su") # 點(diǎn)擊搜索按鈕 search_button.click() # 喘口氣 time.sleep(2) # 斷言結(jié)果 actual_result = self.driver.page_source expect_result = "特朗普" self.assertIn(expect_result, actual_result)
4. 根據(jù)標(biāo)簽名稱進(jìn)行定位
5. 根據(jù)鏈接全部?jī)?nèi)容進(jìn)行定位
6. 根據(jù)鏈接部分內(nèi)容進(jìn)行定位
def test_find_element_by_tag_name(self): # 定位搜索文本框 search_input = self.driver.find_element_by_class_name("s_ipt") # 輸入關(guān)鍵字 search_input.send_keys("馬化騰") # 定位搜索按鈕 search_button = self.driver.find_element_by_id("su") # 點(diǎn)擊搜索按鈕 search_button.click() # 喘口氣 time.sleep(2) # 獲取頁面的返回結(jié)果 # tag_names = self.driver.find_elements_by_tag_name("h3") # for tag_name in tag_names: # print(tag_name.text) # # 通過鏈接的文本信息進(jìn)行定位 # link_text = self.driver.find_element_by_link_text(tag_name.text) # # 對(duì)百度的結(jié)果依次進(jìn)行點(diǎn)擊 # link_text.click() # 根據(jù)部分鏈接文字進(jìn)行定位 pony_infos = self.driver.find_elements_by_partial_link_text("馬化騰") for pony_info in pony_infos: # 依次打印每個(gè)元素的文本信息 print(pony_info.text) # 斷言結(jié)果 actual_result = self.driver.page_source expect_result = "馬化騰" self.assertIn(expect_result, actual_result)
7. 根據(jù)xpath進(jìn)行定位
def test_find_element_by_xpath(self): # 找到搜索輸入框 # search_input = self.driver.find_element_by_xpath('/html/body/div[@id="wrapper"]/div[@id="head"]/div[@class="head_wrapper"]/div[@class="s_form"]/div[@class="s_form_wrapper soutu-env-nomac soutu-env-index"]/form[@class="fm"][@id="form"]/span[@class="bg s_ipt_wr quickdelete-wrap"]/input[@id="kw"][@class="a_ipt"]') search_input = self.driver.find_element_by_xpath('//*[@id="kw"]') # 輸入關(guān)鍵字 search_input.send_keys("天黑請(qǐng)閉眼") # 找到搜索按鈕 # search_button = self.driver.find_element_by_xpath('/html/body/div[@id="wrapper"]/div[@id="head"]/div[@class="head_wrapper"]/div[@class="s_form"]/div[@class="s_form_wrapper soutu-env-nomac soutu-env-index"]/form[@class="fm"][@id="form"]/span[@class="bg s_btn_wr"/input[@id="su"][@class="bg s_btn"]') search_button = self.driver.find_element_by_xpath('//*[@id="su"]') # 點(diǎn)擊搜素按鈕 search_button.click() # 喘口氣 time.sleep(1) # 斷言結(jié)果 expect_value = "天黑請(qǐng)閉眼" actual_value = self.driver.page_source self.assertIn(expect_value,actual_value)
8. 根據(jù)css選擇器進(jìn)行定位
def test_find_element_by_css_selector(self): # search_input = self.driver.find_element_by_css_selector("#kw") search_input = self.driver.find_element_by_css_selector("input#kw") search_input.send_keys("狼人殺") search_button = self.driver.find_element_by_css_selector("input.bg.s_btn") search_button.click() # 喘口氣 time.sleep(1) # 斷言結(jié)果 expect_value = "狼人殺" actual_value = self.driver.page_source self.assertIn(expect_value, actual_value)
總結(jié)
以上所述是小編給大家介紹的python-web根據(jù)元素屬性進(jìn)行定位的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- Python實(shí)現(xiàn)動(dòng)態(tài)給類和對(duì)象添加屬性和方法操作示例
- python GUI庫圖形界面開發(fā)之PyQt5控件QTableWidget詳細(xì)使用方法與屬性
- Python os模塊常用方法和屬性總結(jié)
- python圖形開發(fā)GUI庫pyqt5的詳細(xì)使用方法及各控件的屬性與方法
- python隱藏類中屬性的3種實(shí)現(xiàn)方法
- Python 類的私有屬性和私有方法實(shí)例分析
- python并發(fā)編程 Process對(duì)象的其他屬性方法join方法詳解
- Python面向?qū)ο筇厥鈱傩约胺椒ń馕?/a>
相關(guān)文章
python自動(dòng)化腳本安裝指定版本python環(huán)境詳解
這篇文章主要為大家詳細(xì)介紹了python自動(dòng)化腳本安裝指定版本python環(huán)境的相關(guān)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09在django中使用post方法時(shí),需要增加csrftoken的例子
這篇文章主要介紹了在django中使用post方法時(shí),需要增加csrftoken的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03python中幾個(gè)常用函數(shù)的正確用法-lambda/filter/map/reduce
這篇文章主要介紹了python中幾個(gè)常用函數(shù)的正確用法,這幾個(gè)常用函數(shù)包括lambda、filter、map、reduce,本文將圍繞這幾個(gè)常用函數(shù)展開內(nèi)容,需要的朋友可以參考一下2021-11-11使用python-opencv讀取視頻,計(jì)算視頻總幀數(shù)及FPS的實(shí)現(xiàn)
今天小編就為大家分享一篇使用python-opencv讀取視頻,計(jì)算視頻總幀數(shù)及FPS的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python利用pandas對(duì)數(shù)據(jù)進(jìn)行特定排序
本文主要介紹了Python利用pandas對(duì)數(shù)據(jù)進(jìn)行特定排序,主要使用?pandas.DataFrame.sort_values?方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03使用Windows批處理和WMI設(shè)置Python的環(huán)境變量方法
今天小編就為大家分享一篇使用Windows批處理和WMI設(shè)置Python的環(huán)境變量方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python鍵盤輸入轉(zhuǎn)換為列表的實(shí)例
今天小編就為大家分享一篇Python鍵盤輸入轉(zhuǎn)換為列表的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06