#导入unittest单元测试框架 ##用例的方法前缀必须要以 test 开头的 #这是用来组织用例的 import unittest class TestDBQB(unittest.TestCase): #环境预置 def setUp(self): print("测试预置") #测试用例1,测试用例2 def testCase_1_success(self): print("测试用例1") #断言---检查实际结果和预期结果是否一致 self.assertEqual(True,True)#assert断言 Equal相等 #测试用例2,............ def testCase_2_name_null(self): print("测试用例2") #断言---检查实际结果和预期结果是否一致 try: self.assertEqual(True,False)#assert断言 Equal相等 except: print("得到的结果跟你的预期结果不一致哦!!") #环境恢复 def tearDown(self): print("环境恢复") #代码的执行入口 if __name__=="__main__": unittest.main()
二:python定位元素
①:导包的操作看我之前的写的 Python安装selenium启动浏览器
#1导包 from selenium import webdriver from time import sleep #2:获取浏览器驱动,并打开浏览器 firefox dr =webdriver.Chrome() #打开被测系统 url ="https://www.so.com/" dr.get(url) #2元素定位## #1): 跟剧id定位 find_element_by_id("") inputBox=dr.find_element_by_id("input") inputBox.send_keys("selenium") #自动点击搜索 inputBoxclick=dr.find_element_by_id("search-button") inputBoxclick.send_keys("selenium") inputBoxclick.click() #第二种使用name 来定位 find_element_by_name inputName=dr.find_element_by_name("q") inputName.send_keys("美女") #第三种方法 使用class find_element_by_class_name inputClass=dr.find_element_by_class_name("placeholder") inputClass.send_keys("美女2222222222222222") #第四种方法 定位超链接 find_element_by_link_text("超文本名称全面") aLink=dr.find_element_by_link_text("新闻") aLink.click() #第五种方法 定位超链接 find_element_by_partial_link_text("超文本部分的名字") aLink2=dr.find_element_by_partial_link_text("应用让生产生活更智能") #aLink2.click() #第六 根据标签定位 find_element_by_tag_name() //elements 后面加s 表示有多个 #这里的 \ 表示换行 inputTab_Box=dr.find_elements_by_tag_name("input") for tab in inputTab_Box: if tab.get_attribute("type")==‘text‘ and tab.get_attribute("name")=="q": tab.send_keys("11111111") #第7种 根据绝对路径定位 a="/html/boby/div[2]/div/section[2]/div/form/fieldset/div[2]/input" paths=dr.find_element_by_xpath(a) paths=send_keys("lll") #根据相对路径定位 inputBox1=dr.find_element_by_xpath(‘//input[@type="text"]‘) inputBox1=dr.find_element_by_xpath("//input[@type=‘text‘ and @name=‘q‘]") inputBox1=dr.find_element_by_xpath("//div[@id=‘suggest-align‘]/input[@type=‘text‘]") inputBox1=dr.find_element_by_xpath("//*[@id=‘suggest-align‘]/*[@type=‘text‘ and @name=‘q‘]") iinputBox1=dr.find_element_by_xpath("//fieldset[@id=‘input-container‘]/*[@id=‘suggest-align‘ and @class=‘skin-search-input hover‘]/*[@type=‘text‘ and @name=‘q‘]") iinputBox1.send_keys("ddd") #第8种 根据 css 选择器定位 find_element_by_css_selector() #1):这里的定位id 是用#号表示 iinputBox1=dr.find_element_by_css_selector(‘#input‘) #2):使用class 定位 . iinputBox1=dr.find_element_by_css_selector(‘.placeholder‘) #3):使用索引[] iinputBox1=dr.find_element_by_css_selector(‘input[id="input"]‘) iinputBox1=dr.find_element_by_css_selector(‘[id="input"]‘) #4):通过层级的父子关系定位: find_element_by_css_selector("div >input") inputBox1=dr.find_element_by_css_selector("#input-container #suggest-align >input[type=‘text‘]") inputBox1.send_keys("66666666666666") #5):通过层级与属性组合定位: ‘‘‘有一个父元素,它的标签名叫div,它有一个class 属性值叫skin-search-input, 它有一个子元素,标签名叫input,并且这个子元素的class 属性值叫placeholder。 我们要找的就是具有这么多特征的一个子元素。‘‘‘ find_element_by_css_selector("div.skin-search-input >input.placeholder") find_element_by_css_selector("fieldset #input-container >input#search-button") sleep(2)#睡眠 inputBox1.clear()# 清空 sleep(2) dr.quit() #关闭窗口
②:等待有 :隐式等待,显示等待
原文:https://www.cnblogs.com/zhu520/p/11488967.html