脚本出现异常时会中断执行,想要继续执行就要做异常处理:

1 try ... except .... else   遇到异常显示异常信息; 没有异常继续执行else后面的脚本

2 try ... except ... finally    遇到异常显示异常信息;不管有无异常都会执行finally后面的脚本

#coding:utf-8
#异常 from selenium import webdriver
dr = webdriver.Firefox()
dr.get("http://www.baidu.com") try:
dr.find_element_by_id('kw111').send_keys("异常处理finally") #故意写错id,定位不到元素
dr.find_element_by_id('su').click()
except Exception as e:
print(e)
finally:
print("finally") #不论有无异常都会执行 try:
dr.find_element_by_id('kw111').send_keys("异常处理else")
dr.find_element_by_id('su').click()
except Exception as e:
print(e)
else:
print('else') #无异常执行后续操作,有异常不执行 dr.quit()

Selenium入门19 捕获异常的更多相关文章

  1. 自动化测试Java一:Selenium入门

    From: https://blog.csdn.net/u013258415/article/details/77750214 Selenium入门 欢迎阅读Selenium入门讲义,本讲义将会重点介 ...

  2. Selenium自动化测试Python一:Selenium入门

    Selenium入门 欢迎阅读Selenium入门讲义,本讲义将会重点介绍Selenium的入门知识以及Selenium的前置知识. 自动化测试的基础 在Selenium的课程以前,我们先回顾一下软件 ...

  3. Selenium 入门到精通系列:六

    Selenium 入门到精通系列 PS:Checkbox方法 例子 HTML: <html> <head> <title>测试页面</title> &l ...

  4. Selenium 入门到精通系列:五

    Selenium 入门到精通系列 PS:显式等待.隐式等待.强制等待方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019 ...

  5. Selenium 入门到精通系列:四

    Selenium 入门到精通系列 PS:鼠标右键.鼠标悬停.键盘操作方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019 ...

  6. Selenium 入门到精通系列:三

    Selenium 入门到精通系列 PS:Driver_Element 常用方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2 ...

  7. Selenium 入门到精通系列:二

    Selenium 入门到精通系列 PS:用户登录 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-04-23 16:12 ...

  8. Selenium 入门到精通系列:一

    Selenium 入门到精通系列 PS:控制浏览器窗口大小.前进.后退.刷新 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 20 ...

  9. Selenium入门6 操作元素,获取元素属性

    取元素的text,属性get_attribute,标签名tag_name 操作元素:send_keys输入,click点击,submit提交,clear清除输入 状态判断:is_display,is_ ...

随机推荐

  1. Educational Codeforces Round 11 B

    Description Consider 2n rows of the seats in a bus. n rows of the seats on the left and n rows of th ...

  2. http文件上传/下载

    package unit; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputSt ...

  3. 4 练习: 使用eclipse开发

    1       练习: 使用eclipse开发 1.1  练习目标 本例讲述在使用eclipse如何创建groovy程序. 1.2  创建new Groovy project 本例假设你已经安装好了g ...

  4. 2----scrapy框架之代理and日志级和请求传参

    一.代理 爬虫文件 daili.py class DailiSpider(scrapy.Spider): name = 'daili' #allowed_domains = ['www.xxx.com ...

  5. WebStorm 预览时把浏览器地址localhost 改成IP

    最近在使用WebStorm时,预览网页时地址总是显示的 http://localhost:63342/... ,如果要调试其它设备感觉很不方法,此时肯定首先想到的亲爱的度娘,但是貌似没有真正很解决问题 ...

  6. Unity Unity发布的ios包在iphone上声音小的原因

    实质上声音是从话筒里出来的,未走扬声器. 仔细查找文档发现是PlayerSettings里的设置不当引起的. 在PlayerSettings取消勾选 Prepare iOS for Recording ...

  7. [转]Loading, Editing, and Saving a Text File in HTML5 Using Javascript

    本文转自:http://thiscouldbebetter.wordpress.com/2012/12/18/loading-editing-and-saving-a-text-file-in-htm ...

  8. adb root错误信息adbd cannot run as root in production builds问题解决

    adb root错误信息adbd cannot run as root in production builds问题解决 一.问题描述 1.输入指令 >adb root adbd cannot ...

  9. Classes as objects

    Before understanding metaclasses, you need to master classes in Python. And Python has a very peculi ...

  10. 远程调用历史及代码编写demo

    历史介绍部分: 远程调用大致经过了corba.webservice.二进制跟restful四个阶段 corba时代,corba(Common Object Request Broker Archite ...