# coding: utf-8

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep # 去掉"Chrome正受到自动测试软件的控制。"
options = webdriver.ChromeOptions()
options.add_argument('disable-infobars')
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.baidu.com/")
# 鼠标移动到设置
ActionChains(driver).move_to_element(driver.find_element_by_xpath(".//*[@id='u1']/a[8]")).perform()
sleep(2)
driver.find_element_by_xpath(".//*[@id='wrapper']/div[6]/a[1]").click()
sleep(2)
# 修改每页显示条数--1、定位到下拉框
driver.find_element_by_xpath(".//*[@id='nr']").click()
# 点击下拉框选项
driver.find_element_by_xpath(".//*[@id='nr']/option[2]").click()
sleep(2)
# 点击保存设置
driver.find_element_by_xpath(".//*[@id='gxszButton']/a[1]").click()
sleep(2)
alert = driver.switch_to.alert
print alert.text
alert.accept()

对于下拉框最简单的处理就是两次定位——先定位下拉框->点击,弹出下拉框内容->定位下拉框里面的选项。

上面代码后面的弹出框就是alert(提醒),可以先driver.switch_to.alert切换到alert,然后:

  • alert.text  ——打印alert内容

  • alert.accept()  ——点击确定

  • alert.dismiss()  ——点击取消(如果有)

微信公众号搜索“自动化测试实战”或扫描下方二维码添加关注~~~

《selenium2 python 自动化测试实战》(10)——下拉框和alert的更多相关文章

  1. selenium2 python自动化测试实战(回归测试)

    selenium2 python自动化测试实战 最近接手商城的项目,针对后台测试,功能比较简单,但是流程比较繁多,涉及到前后台的交叉测试.在对整个项目进行第一轮测试完成之后,考虑以后回归测试任务比较重 ...

  2. 船长带你看书——《selenium2 python 自动化测试实战》(1)

    有很多selenium的教程,你没有从头看,当你看的时候已经很难跟上作者的节奏了.现在,你有机会再从头学一次了.从今天开始,船长会从最基本的知识开始,和大家一起学selenium.能不能跟着一起就看大 ...

  3. python+webdriver,选取Select下拉框中的值

    在选择下拉框中的值时遇到了困难,用driver.find_element_by_id("").send_keys("")进行赋值不能成功获取下拉框中的值.   ...

  4. python+selenium七:下拉框、选项框、select用法

    # from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimpo ...

  5. python selenuim如何判断下拉框是否加载出来,超过时间不再等待

    s_flag = True time_start = time.time() while s_flag: doc = etree.HTML(unicode.encode(driver.page_sou ...

  6. Selenium2+Python自动化测试实战

    本人在网上查找了很多做自动化的教程和实例,偶然的一个机会接触到了selenium,觉得非常好用.后来就在网上查阅各种selenium的教程,但是网上的东西真的是太多了,以至于很多东西参考完后无法系统的 ...

  7. 《selenium2 python 自动化测试实战》(15)——调用js控制滚动条等操作

    看代码: # coding=utf-8 from time import sleepfrom selenium import webdriver driver = webdriver.Firefox( ...

  8. 《selenium2 python 自动化测试实战》(14)——下载文件

    说下载文件之前,我再和大家说一下用cookie登录的事,既然我们用cookie登录,那么传过去的cookie肯定是要和对应的网站完全一致的,注意,是包括大小写哦,本来我也是不知道,我用这个方法登录自己 ...

  9. 《selenium2 python 自动化测试实战》(8)——定位iframe

    我们来看一段最早的代码: # coding: utf-8 from selenium import webdriverfrom time import sleep driver = webdriver ...

随机推荐

  1. FlatBuffer入门笔记

    FlatBuffer入门笔记 1 flatbuffer资料 flatbuffer下载地址:https://github.com/google/flatbuffers flatbuffer官方使用文档: ...

  2. 委托---.net4.0提供两个比较重要的委托

    public delegate void Action<[in T1][,in T2][,in T3]......>([T1 t1][,T2 t2][,T3 t3]...) public ...

  3. 《深入理解mybatis原理1》 MyBatis的架构设计以及实例分析

    <深入理解mybatis原理> MyBatis的架构设计以及实例分析 MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简单.优雅.本文主要讲述MyBatis的架构 ...

  4. bind、delegate、on的区别

    on(type,[data],fn) on有三个参数,type代表事件类型,可以为“click"."onchange"."mouseover" dat ...

  5. vue.js的安装部署+cnpm install 安装过程卡住不动----亲测可用

    1.到Node.js的官网下载node node.js的下载地址,下载完成后,我在d盘新建一个文件夹“node”, 安装到node目录下(安装之后环境变量自动配置了,自己无需再配),比如我的安装路径是 ...

  6. js中字符串与数组的相互转换

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. java程序设计基础篇 复习笔记 第七单元&&第八单元

    7.1 int[][] triArray{ {1}, {1,2}, {1,2,3}, }; 7.2 array[2].length 8.1 Unified Modeling Language:UML ...

  8. 通过Fegin远程调用 ,返回JPA Page 对象报错

    Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.sp ...

  9. 如何规划和选择数据库服务器:CPU、内存、磁盘、网络(转)

    转自:http://blog.chinaunix.net/uid-5715-id-2734517.html 学习如何根据业务模型来计算tpcc值,挺有帮助的. 当一个新的业务系统开发完成后,需要在一个 ...

  10. Linux:减号(-)详解

    减号(-) 代表标准输出/标准输入, 视命令而定. “-”代替stdin和stdout的用法 为应用程序指定参数 ps -aux tar -zxf test.tar 一个减号和两个减号 一个减号后面跟 ...