介绍
怎么在python web中根据元素属性进行定位?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
<强> 1。根据属性ID值进行定位强>
def test_find_element_by_id(自我): #,才能定位搜索文本框 时间=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。根据属性类值进行定位强>
def test_find_element_by_class_name(自我): #,才能定位搜索文本框 时间=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。根据属性名称值进行定位强>
def test_find_element_by_name(自我): #,才能定位搜索文本框 时间=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(自我): #,才能定位搜索文本框 时间=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 (“h4") #,才能for tag_name 拷贝tag_names: #才能,,,印刷(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  pony_infos:拷贝 ,,,#,依次打印每个元素的文本信息 ,,,印刷(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(自我): #,才能找到搜索输入框 #,才能search_input =, self.driver.find_element_by_xpath (& # 39;/html/身体/div [@ id=皐rapper"]/div [@ id=癶ead"]/div [@class=癶ead_wrapper"]/div [@class=皊_form"]/div [@class=皊_form_wrapper  soutu-env-nomac soutu-env-index"]/形式[@class=癴m"] [@ id=癴orm"]/跨度(@class=癰g  s_ipt_wr quickdelete-wrap"]/输入[@ id=発w"] [@class=癮_ipt"] & # 39;) search_input 才能=,self.driver.find_element_by_xpath (& # 39;//* [@ id=発w"] & # 39;) #,才能输入关键字 search_input.send_keys才能(“天黑请闭眼“) #,才能找到搜索按钮 null null null null null null null null null null null null null null null null null null null null null怎么在python web中根据元素属性进行定位