1.selenium定位方式,条件:必须是惟一的,若是不知道怎么写,能够右键copy xpath ,这样就会copy出一个 xpath形式的定位代码css
find_element_by_id('kw')html
find_element_by_name()web
find_element_by_class_name()django
find_element_by_tag_name()框架
find_element_by_link_text()less
find_element_by_partial_link_text()函数
find_element_by_xpath()单元测试
find_element_by_css_selector()测试
2. 经过xpath定位方式:ui
find_element_by_xpath("//*[@id='kw' ") 或者find_element_by_xpath("//input[@id='kw' ") #两个//表示某一个标签,*表示任意标签名, 等同于 find_element_by_id('kw')
find_element_by_xpath("//input[@name='wd' ") #两个//表示某一个标签,*表示任意标签名, 等同于 find_element_by_name('wd')
find_element_by_xpath("//input[@class='s_ipt' ")
find_element_by_xpath("//a[text(), '新闻']") #等同于find_element_by_link_text('新闻'), partial_link_text:能够用 find_element_by_xpath("//a[contains(@name, 'hao123')]")
find_element_by_xpath("//input[@autocomplete='off' ")
find_element_by_xpath("//form/span[@class='aaa']/input[2]")
find_element_by_xpath("/html/body/div/div[2]/")
find_element_by_xpath("//form[@id='form' and name='f' and class='fm']/span[2]/input[2]")
3. css_selector定位方式:
find_element_by_css_selector("#kw") # 等于id
find_element_by_css_selector("[name='wd']")
find_element_by_css_selector(".s_ipt") # . 等于class
find_element_by_css_selector("span.s_ipt >input >") # >表示层级关系
4. selenium定位的时候,如百度首页的设置,这种是须要鼠标悬停的,能够用 ActionChains 方法(from selenium.webdriver.common.action_chains import ActionChains), 下拉列表能够用Select方法(from selenium.webdriver.support.select import Select), 当点保存后会有一个提示框(警告框等),能够用Alert(from selenium.webdriver.common.alert import Alert), 语法以下:
setting=dr.find_element_by_link_text("设置")
ActionChains(dr).move_to_element(setting).perform()
selectitem=dr.find_element_by_id("nr")
Select(selectitem).select_by_value('20')
alert=dr.switch_to_alert()
print(alert.text)
alert.accept()
5. 若是定位的时候感受对可是怎么也定位不到,须要研究一下是否是有嵌套表单,如https://mail.qq.com, 须要多表单的切换:
dr.switch_to_frame('login_frame') # switch_to_frame('') 这里能够是id也能够是name
若是点击后弹出一个新的窗口,这时须要切换窗口,涉及到句柄handle:switch_to_window(),具体用法以下:
handle=dr.current_window_handle #获取当前的窗口句柄
handles=dr.window_handles #获取全部的窗口句柄
for handle in handles:
if handle!=handle1:
dr.switch_to_window(handle)
dr.find_element_by_id('nickname').send_keys("aaa")
dr.close() #关掉一个窗口
6. 几个方法:set_window_size(400,600), getattribute(type), is_displayed()
7. 几种等待方法:
显式等待:from selenium.webdriver.support import expected_conditions as EC
element=WebDriverWait(driver, 5, 0.5).until (
EC.presence_of_element_located((By.ID, 'kw'))
)
隐式等待:只须要加一行:dr.implicitly_wait(5)在程序的开头
8.单元测试框架(unittest, pytest), web开发框架(django, falsk)
9. 验证码怎么破?
10. 怎么样读取csv文件:
import csv
datas=csv.reader(open('./testData/user_info.csv', 'r'))
for data in datas:
if data[0]=='username':
continue
print(data[0])
print(data[1])
11.怎么样读取txt文件:
f=open('./testData/user_info.txt','r')
info=f.readlines()
f.close()
for i in range(0,2):
a=info[i].split(';')
username=a[0]
password=a[1]
b=Login(dr)
b.login126(username,password)
12. 怎么样读取xml文件:
13.unittest 特色:
a. 建立的类必须继续unittest.TestCase
b. 建立的方法必须以test开头
c. 断言方法为self.assertEqual(result, 8)
d. 执行测试在if __name__=='__main__'里用unittest.main()
e. setUp(self), tearDown(self)方法来执行每一条测试的开始要作的和最后要作的操做
f. 测试套件:suit=unittest.TestSuite() suit.addTest(类名(‘方法名'))
g.测试运行器:runner=unittest.TextTestRunner() runner.run(suit)
h.discover方法:能够指定哪些文件来执行, suit=unittest.defaultTestLoader.discover("start_dir='xxx', pattern='test*.py')
i. 能够用一些装饰器,好比@unittest.skip("skip') @unittest.skipIf(3>2, 'if true skip') @unittest.skipUnless(3>2, 'if true执行'), @unittest.unexpectedFailure
j. fixture 除了每条case的setUp, tearDown,还有类和module的setUpClass(cls), tearDownClass(cls),setUpModule(), tearDownModule() 方法前须要用@classmethon来装饰
14. pytest:
pytest
will run all files of the form test_*.py or *_test.py in the current directory and its subdirectories. More generally, it follows standard test discovery rules.