今天讲什么
讲什么标题说了,讲selenium的单选、多选、下拉框选项定位。但其实这东西,没什么太多说的,又比较枯燥,那该怎么让这一集selenium的课程变得有趣呢?有请老中医,哈哈…
怎么样,这个野广告做的还不错吧,你们给多少分?至于为什么要自己写一个html的页面,一是懒得找什么网页又有下来框体,又有单/多选。二是最近更多的想练习写一些web界面,最重要的是怕大家觉得selenium系列的文章没意思,所以搞点噱头呗。哈哈…
页面实现起来比较简单,样式用Bootstrap即可,至于这个城中村经典广告牌,只要简单的使用jQuery添加和删除css样式就OK了。
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>老中医</title>
<link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<style type="text/css">
body {
height: 500px;
}
.container {
max-width: 500px;
height: 450px;
border: 2px solid grey;
border-radius: 5px;
}
.ad {
text-align: center;
font-size: 25px;
background-color: black;
color: red;
margin-bottom: 20px;
}
.chuibi {
display: inline-block;
}
.adplus {
color: black;
}
.footer {
text-align: center;
}
</style>
</head>
<body>
<form class="container">
<div class="row">
<div class="ad ">祖传男科老中医
<div class="small chuibi"> 【华佗在世,扁鹊转生】
</div>
</div>
</div>
<div class="form-group ">
<div class="input-group">
<div class="input-group-addon">
姓名:
</div>
<input id='name' class="form-control" />
</div>
</div>
<div class="form-group ">
<div class="input-group">
<div class="input-group-addon">
性别:
</div>
<select id="sex" class="form-control">
<option value="女">女</option>
<option value="男">男</option>
</select>
</div>
</div>
<div class="form-group has-success">
<div><label class="control-label">如何了解我院:</label></div>
<label class="radio-inline">
<input type="radio" name="access" id="inlineRadio2" value="朋友介绍"> 朋友介绍
</label>
<label class="radio-inline">
<input type="radio" name="access" id="inlineRadio1" value="野广告">野广告
</label>
</div>
<div class="form-group has-error">
<div><label class="control-label">有哪些病症:</label></div>
<div class="checkbox">
<label>
<input type="checkbox" value="全身乏力">
全身乏力
</label>
</div>
<div class="checkbox ">
<label>
<input type="checkbox" value="食欲不振">
食欲不振
</label>
</div>
<div class="checkbox ">
<label>
<input type="checkbox" value="腰膝酸软">
腰膝酸软
</label>
</div>
<div class="checkbox ">
<label>
<input type="checkbox" value="失眠多梦">
失眠多梦
</label>
</div>
</div>
<div class="form-group">
<div><label class="control-label">简要描述你的病情:</label></div>
<input id='summary' class="form-control" type="text">
</div>
</form>
<div class="footer">
©2019-清风Python:<a href="https://www.jianshu.com/u/d23fd5012bed">联系我们</a>
</div>
<script>
setInterval(function() {
var title = $('.ad')
if (title.hasClass('adplus')) {
title.removeClass('adplus')
} else { $('.ad').addClass('adplus') }
}, 600);
</script>
</body>
</html>
入主题
先来说说下拉框吧,网上很多的帖子说下拉框的选择必须要先定位到下拉框体,然后点击框体,才能获取选项并选中,其实不然。90%的下拉框都会提前将option内容保存在html中,只有极少情况时通过AJAX请求的(这种情况之后的课程说…)。所以针对这种选项类的使用,推荐使用xpath和css_selector去定位。因为可变概率较小…
至于单选和多选,主要是通过driver.find_elements_by_tag_name('input'),然后进行批量循环判断input选项完成选择。
说了这么多,不如直接看代码来的直观:
# -*- coding: utf-8 -*-
# @Author : 王翔
# @JianShu : 清风Python
# @Date : 2019/6/22 22:04
# @Software : PyCharm
# @version :Python 3.7.3
# @File : Day_3.1_select.py
from selenium import webdriver
import time
# 本地文件,根据你们的位置,自行修改
URL = 'file:///D:/Codes_Repository/Python/SeleniumTest/Day4_selenium定位下拉框/index.html'
options = webdriver.ChromeOptions()
# 调整窗口大小,主要是为了使减小gif文件大小,方便截图上传
options.add_argument('window-size=650,650')
options.add_argument('disable-infobars')
driver = webdriver.Chrome(options=options)
driver.get(URL)
driver.find_element_by_id('name').send_keys("隔壁老王")
time.sleep(1)
sex = driver.find_element_by_id('sex')
sex.find_element_by_css_selector("[value='男']").click()
# driver.find_element_by_xpath('//*[@id="sex"]/option[2]').click()
time.sleep(1)
input_tags = driver.find_elements_by_tag_name('input')
for input_tag in input_tags:
if input_tag.get_attribute('type') == 'radio' and input_tag.get_attribute('value') == '野广告':
input_tag.click()
time.sleep(1)
input_tags = driver.find_elements_by_tag_name('input')
for input_tag in input_tags:
if input_tag.get_attribute('type') == 'checkbox' and input_tag.get_attribute('value') != '腰膝酸软':
input_tag.click()
time.sleep(2)
driver.close()
看下隔壁老王前来就诊的效果:
由于页面只有一组单选和多选,所以无需多做判断,但如果网页上存在多组的单选和多选。那还需要针对他们自身的特性进行分析,知识是死的,要学会灵活运用才行…
To Be Continue
今天的内容就到这里,如果觉得有帮助,欢迎将文章或者我的公众号【清风Python】分享给更多喜欢python的人
来源:华为云社区征文 作者:清风Python
大型情感剧集Selenium:4_老中医教你(单/多/下拉框)选项定位 #华为云·寻找黑马程序员#的更多相关文章
-
大型情感剧集Selenium:3_元素定位 #华为云·寻找黑马程序员#
关于昨天的文章 今天有朋友反馈,代码运行的时候,selenium提示警告 DeprecationWarning: use options instead of chrome_options drive ...
-
大型情感剧集Selenium:2_options设置 #华为云·寻找黑马程序员#
上集回顾 昨天说简单介绍了什么是selenium,它能干what,和发展史与梗概.当的是python如何通过pip安装selenium,并下载对应浏览器的webdriver. 最后简单通过一个Demo ...
-
大型情感剧集Selenium:6_selenium中的免密登陆与cookie操作 #华为云·寻找黑马程序员#
欢迎添加华为云小助手微信(微信号:HWCloud002 或 HWCloud003),输入关键字"加群",加入华为云线上技术讨论群:输入关键字"最新活动",获取华 ...
-
大型情感剧集Selenium:1_介绍 #华为云·寻找黑马程序员#
学习selenium能做什么? 很多书籍.文章中是这么定义selenium的: Selenium 是开源的自动化测试工具,它主要是用于Web 应用程序的自动化测试,不只局限于此,同时支持所有基于web ...
-
大型情感剧集Selenium:8_selenium网页截图的四种方法
有时候,有时候,你会相信一切有尽头-当你的代码走到了尽头,那么保留最后一刻的状态尤为重要,此时你该如何操作?记录日志-没有将浏览器当前的状态进行截图来的直观! 那么,selenium截取截屏,有哪些方 ...
-
大型情感剧集Selenium:9_selenium配合Pillow完成浏览器局部截图
网页截图 上次提到了selenium的四种截图方法,最终截图了整张网页.但很多时候,我们仅仅需要截图部分的内容.比如截取某个关键信息,或者现在已经不常见的截图验证码(现在都是各种按规则点击-).那么我 ...
-
大型情感剧集Selenium:6_selenium中的免密登陆与cookie操作
网站登录 现在各大平台在反爬虫功能上,可谓花样繁多.总结下来按照破解成功率排名,最高的是滑动解锁.其次是验证码数字.之后是一次点击对应的汉字,最后是想12306之前那种反人类的让你说那些是奶糖吧,哈哈 ...
-
大型情感剧集Selenium:6_selenium中的免密登陆与cookie操作【华为云技术分享】
网站登录 现在各大平台在反爬虫功能上,可谓花样繁多.总结下来按照破解成功率排名,最高的是滑动解锁.其次是验证码数字.之后是一次点击对应的汉字,最后是想12306之前那种反人类的让你说那些是奶糖吧,哈哈 ...
-
Selenium常用操作汇总二——如何操作select下拉框
下面我们来看一下selenium webdriver是如何来处理select下拉框的,以http://passport.51.com/reg2.5p这个页面为例.这个页面中有4个下拉框,下面演示4种选 ...
随机推荐
-
mysql: java.sql.SQLException: Incorrect string value: &#39;\xF0\x9F\x92\x90<;/...&#39;
插入数据出现问题,因为包含了特殊字符. 现象: 插入的数据中如果含有某些特殊字符,会导致插入数据失败,例如字符串”测试**插入数据...“,在console中insert是正常的,但是使用java代码 ...
-
Transaction Manager Maximum Timeout
TransactionManager.MaximumTimeout是个只读的属性, 默认只有10分钟, 要想修改它必须通过machine.config来修改. 为了单个应用而去修改这个值是不合适的. ...
-
Error: Cannot find module &#39;babel-runtime/regenerator&#39;
在做调用阿里云短信接口时遇到的一个问题 错误原因:没有正确安装相应的mmodule 解决办法: 第一步:在package.json中加入依赖label-runtime 第二步:在Terminal中 n ...
-
ext 的controller中的refs的使用方法
通过ext api 可以知道ext 的controller中有个refs的属性,对于这个属性 文档上是这么说的:配置数组构建页面上的视图的引用. 我并看不懂,接下来说的是我对这个refs的理解. 对这 ...
-
Thinkphp 去除bom头 解决模版空输出问题
<?php if (isset($_GET['dir'])){ //config the basedir $basedir=$_GET['dir']; }else{ $basedir = '.' ...
-
Java面向对象之异常(自定义异常)
一.基础概念 在自定义的程序中,如果有了问题.也可以像java中的异常一样,对问题进行描述. 注意:1.继承RuntimeException的异常,不需要进行处理.在执行过程中有异常会直接抛出. 2. ...
-
svn 未提交的显示黑色的星*
1.在eclipse中,选择window-->Preferences,里面找到svn,如下图,勾选上Outgoing changes即可
-
java接口调试思想
对于接口调试的理解:最近多次参与接口调试工作,一般情况都是获取对方接口文档,文档中有加密验证方式,根据加密验证方式开发,调用对应的接口.可以不可以简化这个流程那,至少减少一方的工作量.1.减少调用方法 ...
-
【二分答案】【分块答案】【字符串哈希】【set】bzoj2946 [Poi2000]公共串
我们二分/分块枚举答案x,暴力把除了最短的字符串以外的其他字符串的x长度子串哈希搞出来,分别扔到set里. 然后暴力枚举最短的字符串的x长度字串,查看是否在全部的set里出现过. #include&l ...
-
对jsp中Url含中文字符的编码处理
有一段url="/app/index/index.jsp?userName='测试'":在传入到jsp页面后. 用 <% String userName=request.g ...