政务类网站,wordpress 主题 轮播,wordpress响应式模板,成都网站建设哪家强周末实验自动化提交数据时#xff0c;本来没打算写记录的#xff0c;不过遇到一些问题#xff0c;觉得可以提提。基本操作就不用写了#xff0c;搜索过程中都发现了两个博客都出了seleniumpython的书#xff0c;说明操作一搜一大把。
1. 等待页面加载完成
本来用的sleep()…周末实验自动化提交数据时本来没打算写记录的不过遇到一些问题觉得可以提提。基本操作就不用写了搜索过程中都发现了两个博客都出了seleniumpython的书说明操作一搜一大把。
1. 等待页面加载完成
本来用的sleep(),自己判断的有的需要时间长点就给3-4秒时间短的页面内等待就0.5秒。但有时候网络不稳定可能需要的时间更长就会抛错。发现文档中有说
显式等待是你在代码中定义等待一定条件发生后再进一步执行你的代码。 最糟糕的案例是使用time.sleep()它将条件设置为等待一个确切的时间段。
尴尬-! 但是最后我还是采用了sleep()以后需要优化再改吧。
一些跳转链接的地方我用了 driver.current_url来获取当前URL和预期的作对比来完成操作。
2.开左右两个窗口各占一半屏
使用maximize_window将浏览器最大化然后用get_window_size获取到宽高。接下来就只是简单的用set_window_rect置左右窗口位置和大小了。
def start(self, alignNone):配置并启动
option webdriver.ChromeOptions()
option.add_argument(disable-infobars) # 无提醒
#option.add_argument(headless) # 静默运行
self.driver webdriver.Chrome(chrome_optionsoption)
# 置屏幕左右
if align:
# 全屏
self.driver.maximize_window()
# 获取大小
size self.driver.get_window_size()
width size[width]
height size[height]
if align left:
self.driver.set_window_rect(0, 0, int(width/2), height)
elif align right:
self.driver.set_window_rect(int(width/2), 0, int(width/2), height)
else:
pass
return self.driver
3. 多线程
上面明显是一个执行之后再另外一个刚好需要加多线程正好测试测试。 类中的方法加多线程试出来一种办法
study StudyMap(left)
study2 StudyMap(right)
# for i in range(10):
# study.addCompleteMap()
#构建线程
threads []
t Thread(targetstudy.addCompleteMap)
threads.append(t)
t Thread(targetstudy2.addCompleteMap)
threads.append(t)
#启动所有线程
for t in threads:
t.start()
先初始化两个类这个过程是单线程的之后调用addCompleteMap方法使用多线程。ok。
想多次执行addCompleteMap函数怎么办呢比如上面我注释掉的
for i in range(10):
study.addCompleteMap()
加个函数试试
study StudyMap(left)
study2 StudyMap(right)
def runx(cls):
for i in range(10):
cls.addCompleteMap()
# 构建线程
threads []
t Thread(targetrunx, args(study,))
threads.append(t)
t Thread(targetrunx, args(study2,))
threads.append(t)
# 启动所有线程
for t in threads:
t.start()
可以的将runx方法多线程参数为类对象。所以不管是普通方法还是类中的成员方法用多线程来实现没有多大区别。这不都转了一例。具体更深入的问题留到以后研究或者遇到了再研究吧。
4. 多线程取数据问题
上面的还有问题业务中第二步有用数据两个选择了同样的数据不能保存成功。以及有的页面小屏看不到确定按钮。没打算自动化测试只是想填数据。还真给测出了BUG
1) 针对数据混乱问题加锁图多线程调用数据重复线程加锁前
lock threading.Lock()
# 选择试卷需要加锁
lock.acquire()
logging.info([获取试卷])
... ... 其他逻辑处理
logging.info([保存])
lock.release()图多线程加锁后数据调用完整
加锁导致速度变慢考虑取数据时可以分开取第一个线程取第1行第二个线程取第2行也可以就取了锁。用线程名取不同的数据。为了避免数据不够最后多备1条数据。还有其他业务需同时取3条数据同理。
线程加name
# 构建线程
threads []
t Thread(targetrunx, args(study,), namet1)
threads.append(t)
t Thread(targetrunx, args(study2,), namet2)
threads.append(t)
判断获取不同的数据为了清晰在另外一处业务取不同的3条数据处来查看
原写法
self.driver.find_element_by_xpath(//table[starts-with(ng-table-dynamic,tableParams)]/tbody/tr[1]).click()
self.driver.find_element_by_xpath(//table[starts-with(ng-table-dynamic,tableParams)]/tbody/tr[2]).click()
self.driver.find_element_by_xpath(//table[starts-with(ng-table-dynamic,tableParams)]/tbody/tr[3]).click()
修改为根据线程获取不同数据
if threading.current_thread().name t1:
tn 1
else:
tn 4
# 线程1-取123行, 2-456行
self.driver.find_element_by_xpath(//table[starts-with(ng-table-dynamic,tableParams)]/tbody/tr[{0}].format(tn)).click()
self.driver.find_element_by_xpath(//table[starts-with(ng-table-dynamic,tableParams)]/tbody/tr[{0}].format(tn1)).click()
self.driver.find_element_by_xpath(//table[starts-with(ng-table-dynamic,tableParams)]/tbody/tr[{0}].format(tn2)).click()图不用锁不同线程选择不同数据
2) 针对按钮看不到放全屏操作完后再还原
为了重置窗口大小将1节中的方法拆分
from selenium import webdriver
from time import sleep
class AutoSubmit():自动提交测试
def __init__(self):
self.rect (0, 0, 800, 600)
pass
def start(self, alignNone):配置并启动
logging.info(配置并启动Chrome)
option webdriver.ChromeOptions()
option.add_argument(disable-infobars) # 无提醒
#option.add_argument(headless) # 静默运行
self.driver webdriver.Chrome(chrome_optionsoption)
# 置屏幕左右
if align:
# 全屏
self.driver.maximize_window()
# 获取大小
size self.driver.get_window_size()
width size[width]
height size[height]
if align left:
self.rect(0, 0, int(width/2), height)
elif align right:
self.rect(int(width/2), 0, int(width/2), height)
else:
pass
# 设置窗口
self.setWindow()
return self.driver
def setWindow(self, *, maxWindowFalse, minWindowFalse):设置窗口位置和大小
if maxWindow:
self.driver.maximize_window()
elif minWindow:
self.driver.minimize_window()
else:
self.driver.set_window_rect(*self.rect)
start()中左右屏幕只改变self.rect变量调用setWdindow()方法重置大小不传参则为当前rect大小传maxWindow/minWindow则放大缩小。
调用打开左右两个窗口
# 左右两个窗口
driver1 a1.start(left)
driver2 a2.start(right)
窗口2全屏加载bing首页并置原大小
# 需要全屏时
a2.setWindow(maxWindowTrue)
driver2.get(http://bing.com)
sleep(2)
a2.setWindow()参考资料