python-web根據(jù)元素屬性進行定位的方法
1. 根據(jù)屬性ID值進行定位
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") # 點擊搜索按鈕 search_button.click() # 喘口氣 time.sleep(2) # 斷言結(jié)果 actual_result = self.driver.page_source expect_result = "馬云" self.assertIn(expect_result, actual_result)
2. 根據(jù)屬性CLASS值進行定位
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") # 點擊搜索按鈕 search_button.click() # 喘口氣 time.sleep(2) # 斷言結(jié)果 actual_result = self.driver.page_source expect_result = "奧巴馬" self.assertIn(expect_result, actual_result)
3. 根據(jù)屬性NAME值進行定位
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") # 點擊搜索按鈕 search_button.click() # 喘口氣 time.sleep(2) # 斷言結(jié)果 actual_result = self.driver.page_source expect_result = "特朗普" self.assertIn(expect_result, actual_result)
4. 根據(jù)標簽名稱進行定位
5. 根據(jù)鏈接全部內(nèi)容進行定位
6. 根據(jù)鏈接部分內(nèi)容進行定位
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") # 點擊搜索按鈕 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) # # 通過鏈接的文本信息進行定位 # link_text = self.driver.find_element_by_link_text(tag_name.text) # # 對百度的結(jié)果依次進行點擊 # link_text.click() # 根據(jù)部分鏈接文字進行定位 pony_infos = self.driver.find_elements_by_partial_link_text("馬化騰") for pony_info in pony_infos: # 依次打印每個元素的文本信息 print(pony_info.text) # 斷言結(jié)果 actual_result = self.driver.page_source expect_result = "馬化騰" self.assertIn(expect_result, actual_result)
7. 根據(jù)xpath進行定位
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("天黑請閉眼") # 找到搜索按鈕 # 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"]') # 點擊搜素按鈕 search_button.click() # 喘口氣 time.sleep(1) # 斷言結(jié)果 expect_value = "天黑請閉眼" actual_value = self.driver.page_source self.assertIn(expect_value,actual_value)
8. 根據(jù)css選擇器進行定位
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ù)元素屬性進行定位的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
python自動化腳本安裝指定版本python環(huán)境詳解
這篇文章主要為大家詳細介紹了python自動化腳本安裝指定版本python環(huán)境的相關(guān)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09在django中使用post方法時,需要增加csrftoken的例子
這篇文章主要介紹了在django中使用post方法時,需要增加csrftoken的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03python中幾個常用函數(shù)的正確用法-lambda/filter/map/reduce
這篇文章主要介紹了python中幾個常用函數(shù)的正確用法,這幾個常用函數(shù)包括lambda、filter、map、reduce,本文將圍繞這幾個常用函數(shù)展開內(nèi)容,需要的朋友可以參考一下2021-11-11使用python-opencv讀取視頻,計算視頻總幀數(shù)及FPS的實現(xiàn)
今天小編就為大家分享一篇使用python-opencv讀取視頻,計算視頻總幀數(shù)及FPS的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python利用pandas對數(shù)據(jù)進行特定排序
本文主要介紹了Python利用pandas對數(shù)據(jù)進行特定排序,主要使用?pandas.DataFrame.sort_values?方法,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2024-03-03使用Windows批處理和WMI設(shè)置Python的環(huán)境變量方法
今天小編就為大家分享一篇使用Windows批處理和WMI設(shè)置Python的環(huán)境變量方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08