五 Selenium常用API((单/多/下拉框)选项定位,iframe相关操作,alert\confirm\prompt)

一  (单/多/下拉框)选项定位

    90%的下拉框都会提前将option内容保存在html中,只有极少情况时用过AJAX请求的.所以针对这种选项类,推荐使用xpath和css_selector去定位(当前也可以选择其他的方式定位).因为可变概率较小...

    至于单选和多选,主要是通过driver.find_elements_by_tag_name('input'),然后进行批量循环判断input选项完成选择。

<!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>
练习的html

   下拉框

options = webdriver.ChromeOptions()
#屏蔽“Chrome正受到自动测试软件的控制”提示信息
options.add_experimental_option("excludeSwitches", ['enable-automation'])
driver = webdriver.Chrome(chrome_options=options)

driver.get(r"D:\代码\autotest\user\templates\test.html")
select = Select(driver.find_element_by_id('sex'))
#通过index来选择(从0开始)
select.select_by_index(1)
#通过value值
select.select_by_value('')
#通过text选择
select.select_by_visible_text('')
示例
#Select的其他方法
deselect_all() :取消所有选项
deselect_by_index() :取消对应 index 选项
deselect_by_value() :取消对应 value 选项
deselect_by_visible_text() :取消对应文本选项
first_selected_option() :返回第一个选项
all_selected_options() :返回所有的选项

   单选框

options = webdriver.ChromeOptions()
#屏蔽“Chrome正受到自动测试软件的控制”提示信息
options.add_experimental_option("excludeSwitches", ['enable-automation'])
driver = webdriver.Chrome(chrome_options=options)

driver.get(r"D:\代码\autotest\user\templates\test.html")
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()
示例

   多选框

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()
示例

注:单选框和多选框都可以使用is_selected(),判断是否选中

二 iframe相关操作

   frame与iframe两者可以实现的功能基本相同,不过iframe和frame具有更多的灵活性.frame是整个页面的框架,iframe是内嵌的网页元素.iframe标记又叫做浮动帧标记,可以用它将一个HTML文档嵌入在一个HTML中显示它和 Frame 标记的最大区别是在网页中嵌入 的<iframe></iframe>所包含的内容与整个页面是一个整体,而<frame>< /frame>所包含的内容是一个独立的个体,是可以独立显示的。另外,应用 iframe 还可以在同一个页面中多次显示同一内容,而不必重复这段内容的代码.

#默认支持id和name,若没有这两个属性,则先用八种定位方式,定位到iframe,再切换
driver.switch_to.frame()
#释放 iframe
switch_to_default_content()

三 alert\confirm\prompt

   如下图依次为

 

 

#text:获取文本值
#accept() :点击"确认"
#dismiss() :点击"取消"或者叉掉对话框
#send_keys() :输入文本值 --仅限于 prompt,在 alert 和 confirm 上没有输入框

注:在操作alert\confirm\prompt之前,先用switch_to_alert()方法切换到 alert 弹出框上

示例:

 

posted on 2020-03-21 22:54  rwwh  阅读(220)  评论(0)    收藏  举报

导航