Posted in Python onDecember 13, 2019
1. 根据属性ID值进行定位
def test_find_element_by_id(self): # 定位搜索文本框 search_input = self.driver.find_element_by_id("kw") # 输入关键字 search_input.send_keys("马云") # 定位搜索按钮 search_button = self.driver.find_element_by_id("su") # 点击搜索按钮 search_button.click() # 喘口气 time.sleep(2) # 断言结果 actual_result = self.driver.page_source expect_result = "马云" self.assertIn(expect_result, actual_result)
2. 根据属性CLASS值进行定位
def test_find_element_by_class_name(self): # 定位搜索文本框 search_input = self.driver.find_element_by_class_name("s_ipt") # 输入关键字 search_input.send_keys("奥巴马") # 定位搜索按钮 search_button = self.driver.find_element_by_id("su") # 点击搜索按钮 search_button.click() # 喘口气 time.sleep(2) # 断言结果 actual_result = self.driver.page_source expect_result = "奥巴马" self.assertIn(expect_result, actual_result)
3. 根据属性NAME值进行定位
def test_find_element_by_name(self): # 定位搜索文本框 search_input = self.driver.find_element_by_name("wd") # 输入关键字 search_input.send_keys("特朗普") # 定位搜索按钮 search_button = self.driver.find_element_by_id("su") # 点击搜索按钮 search_button.click() # 喘口气 time.sleep(2) # 断言结果 actual_result = self.driver.page_source expect_result = "特朗普" self.assertIn(expect_result, actual_result)
4. 根据标签名称进行定位
5. 根据链接全部内容进行定位
6. 根据链接部分内容进行定位
def test_find_element_by_tag_name(self): # 定位搜索文本框 search_input = self.driver.find_element_by_class_name("s_ipt") # 输入关键字 search_input.send_keys("马化腾") # 定位搜索按钮 search_button = self.driver.find_element_by_id("su") # 点击搜索按钮 search_button.click() # 喘口气 time.sleep(2) # 获取页面的返回结果 # 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) # # 对百度的结果依次进行点击 # link_text.click() # 根据部分链接文字进行定位 pony_infos = self.driver.find_elements_by_partial_link_text("马化腾") for pony_info in pony_infos: # 依次打印每个元素的文本信息 print(pony_info.text) # 断言结果 actual_result = self.driver.page_source expect_result = "马化腾" self.assertIn(expect_result, actual_result)
7. 根据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"]') # 输入关键字 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) # 断言结果 expect_value = "天黑请闭眼" actual_value = self.driver.page_source self.assertIn(expect_value,actual_value)
8. 根据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) # 断言结果 expect_value = "狼人杀" actual_value = self.driver.page_source self.assertIn(expect_value, actual_value)
总结
以上所述是小编给大家介绍的python-web根据元素属性进行定位的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
python-web根据元素属性进行定位的方法
- Author -
MeriaChen声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@