一、搜索参数化
在TXT文件中保存需要搜索的内容:
测试代码:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # @File : txt.py 4 # @Software: PyCharm 5 from selenium import webdriver 6 import time 7 8 url = 'https://www.baidu.com' 9 browser = webdriver.Firefox() 10 browser.get(url) 11 # 打开test文件 12 file = open('C:\Users\Administrator\Desktop\test.txt') 13 # 逐行读取整个文件内容 14 lines = file.readlines() 15 16 for i in lines: 17 browser.find_element_by_id('kw').send_keys(i) 18 browser.find_element_by_id('su').click() 19 time.sleep(3) 20 browser.close() 21 file.close() 22 browser.quit()
二、登录参数化
测试代码:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # @File : txts.py 4 # @Software: PyCharm 5 from selenium import webdriver 6 import time 7 8 url = 'http://www.yeah.net/' 9 browser = webdriver.Firefox() 10 browser.get(url) 11 12 file = open('C:\Users\Administrator\Desktop\test.txt') 13 line = file.readline() 14 (username, password) = line.strip(' ').split(',') 15 16 browser.switch_to.frame('x-URS-iframe') 17 browser.find_element_by_name('email').send_keys(username) 18 browser.find_element_by_name('password').send_keys(password) 19 browser.find_element_by_id('dologin').click() 20 time.sleep(5) 21 22 browser.switch_to.default_content() 23 # 简单判断登录是否成功 24 name = browser.find_element_by_id("spnUid").text 25 print(name) 26 if name == 'sanzang520@yeah.net': 27 print(u'登录成功') 28 else: 29 print(u'登录失败') 30 browser.find_element_by_link_text('退出').click() 31 browser.quit()