selenium使用过程中的坑
- 使用class标签定位时报错:
message: no such element: Unable to locate element: {"method":"class name"
在测试过程中,定位一个验证码图片时,由于执行速度过快,浏览器还没有接收到服务端发出的信息,导致无法找到该class信息。
解决方法:
1.
def drag_code(self):
time.sleep(0.5) #在执行查询之前,加一个延时time.sleep(0.5) #在执行查询之前,加一个延时
image_info = self.track.get_image()
增加延时简单粗暴,但是并不灵活。
2.
self.driver.implicitly_wait(10)
hehe = self.driver.find_element_by_class_name("verify-img-panel")
增加一个自动检测,如果10秒内还未成功,则报错。假如后续网站访问速度加快,则可以减少程序执行等待的时间。