博主在尝试对百度首页用selenium完成自动登录的功能

反复多次尝试元素定位方法也未写错、最后发现问题原因:

脚本运行速度快于页面加载速度

如百度首页登录例子、脚本已经开始寻找登录弹窗

但是页面仍在加载、导致程序报错

博主就整理下智能等待的方法

首先是显示等待:即等待页面某个元素出现、超时则抛出错误

 #!/usr/bin/env python
# -*- coding: utf_8 -*- from learn_webdriver import Webdriver
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep browser_chrome = webdriver.Chrome(Webdriver.chrome())
browser_chrome.get("http://www.baidu.com") browser_chrome.find_element_by_css_selector("html body div#wrapper div#head div.head_wrapper div#u1 a.lb").click()
# 定位百度首页中登录按钮元素
WebDriverWait(browser_chrome, 10).until(lambda the_driver:
browser_chrome.find_element_by_class_name("tang-foreground").is_displayed())
browser_chrome.find_element_by_class_name("tang-foreground").find_element_by_name("userName").send_keys(u"baidu用户名")
# 定位到登录窗口后再定位到用户名输入框
browser_chrome.find_element_by_name("password").send_keys(u"baidu密码")
sleep(2)
print browser_chrome.title
browser_chrome.quit()

这里主要是使用 WebDriverWait() 类构建方法、直到找到登录弹窗元素为止

下面是隐形等待:即设置等待时间、等待页面完成新的操作后、若超时则抛出错误

 #!/usr/bin/env python
# -*- coding: utf_8 -*- from learn_webdriver import Webdriver
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep browser_chrome = webdriver.Chrome(Webdriver.chrome())
browser_chrome.get("http://www.baidu.com") browser_chrome.find_element_by_css_selector("html body div#wrapper div#head div.head_wrapper div#u1 a.lb").click()
# 定位百度首页中登录按钮元素
browser_chrome.implicitly_wait(30)
# 添加智能等待时间
div_user = browser_chrome.find_element_by_class_name("tang-foreground").find_element_by_name("userName")
div_user.send_keys(u"baidu用户名")
div_pass = browser_chrome.find_element_by_name("password").send_keys(u"baidu密码")
sleep(2)
print browser_chrome.title
browser_chrome.quit()

implicitly方法智能设置等待时间30S、超时则抛出异常

selenium学习笔记(智能等待)的更多相关文章

  1. Selenium 学习笔记(一)

    selenium 学习整理 初学者,如果有不当得地方请指出,非常感谢. 准备事项: 1. Python 安装包 安装Python,并勾选添加环境变量. 安装完成后,打开dos窗口,输入python,看 ...

  2. python + selenium 学习笔记 -摘要

    一.浏览器操作相关 from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() # 窗口最大 ...

  3. selenium学习笔记(HTMLTestRunner测试报告)

    之前提到selenium加入unittest框架.可以引入HTMLTestRunner扩展.以此来生成测试报告 首先是分享下载的百度云地址 http://pan.baidu.com/s/1pKUItW ...

  4. selenium学习笔记(加入unittest)

    利用firefox浏览器的selenium IDE可以直接生成webdriver+unittest的python脚本 当然博主是要为了自己编写脚本.对用例内容进行了修改,把元素校验功能也放入了用例中 ...

  5. selenium 学习笔记 ---新手学习记录(1) 问题总结

    说明:每次学习各种语言时,环境搭建访问国外网址最头疼了,现在只要是工具下载好放到自己网盘,可以随时用. 1.首先工具准备,selenium需要用到的 下载地址 访问密码 ff8f 2.我选择的语言时j ...

  6. selenium学习笔记(selenium IDE下载安装)

    今天自己一直在瞎捣鼓 最后这里整理下 selenium IDE 这个录制工具的下载安装 首先这个工具只支持火狐浏览器firefox.使用火狐浏览器进入selenium官网: http://www.se ...

  7. selenium学习笔记(简单的元素定位)

    收拾一下心情开始新的一周工作 继续是selenium的学习.配置成功后 由于所有操作都是建立在页面元素基础上的.所以下来就是学习定位元素 首先是基础的定位.就使用博客园首页搜索框为例: 下面是代码: ...

  8. selenium学习笔记(selenium下载安装)

    博主自己捣鼓的接口框架先到这里 等工作上正式开始使用再后续完善需求 还是继续学习python.学编程就直接动手写 就想看看python+selenium的组合 什么都不多说.先下载安装 博主这里已经安 ...

  9. Web自动化测试Selenium 学习笔记(一)

    1.Web自动化测试简介自动化基础:自动化用例编写.Selenium优势及原理.自动化环境搭建Selenium基础:常见8大元素定位(表格).常见元素处理.下拉框元素处理.不同窗口切换.元素进阶.元素 ...

随机推荐

  1. 高德地图 js api 使用

    使用高德地图js api 制作网页上的地图应用. 1.先申请一个 开发者用的 key . 2. 在页面中引入高德提供的地图js  <script src="http://webapi. ...

  2. git 从远程仓库指定分支clone代码到本地

    不指定分支 git clone + clone 地址 # 例如 git clone https://amc-msra.visualstudio.com/xxx/_xx/xxxxxx 指定分支 git ...

  3. java jacob调用打印,word,excel横向打印

    public static boolean printOfficeFile(File f) { if (f != null && f.exists()) { String fileNa ...

  4. Kettle-1-安装配置

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wl101yjx/article/details/32921691 写在前面一: 数据仓库ETL工具有 ...

  5. 主机名 域名 网站名 URL

    举几个域名的例子:google.com,baidu.com,163.com可以明确的告诉你,加上www,就不再是域名了! 以http://mail.163.com/index.html为例进行说明:1 ...

  6. 人性化的Form(django)

    django中的Form一般有两种功能: 输入html 验证用户输入 html: <!DOCTYPE html> <html lang="en"> < ...

  7. python 对象和类

    python中所有数据都是以对象形式存在.对象既包含数据(变量),也包含代码(函数),是某一类具体事物的特殊实例. 面向对象的三大特性为封装.继承和多态. 1.定义类 #定义空类 class Pers ...

  8. Windows Live Writer 网易博客配置

    一.去官网下载 Windows Live Write 组件 二.配置,选中其它服务,然后会到如下界面 主要是这里经常不知道选择什么,容易忘记. 下拉框选中metaweblog API 类型,把这个地址 ...

  9. 第二课客户端链接Linux系统

    使用Putty客户端软件连接Linux主机 使用rpm –qa | grep ssh命令查看是否已经安装ssh服务,如下图是已经安装了ssh服务,如果未列出相关的openssh那么代表未安装这时候就需 ...

  10. ceph存储安装配置

    1.修改yum源: 1.安装yum源:sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://dl.fedor ...