zoukankan      html  css  js  c++  java
  • Selenium-执行js

    可以执行任何js

    语法:driver.execute_script("js")

    注意:Python代码和HTML代码保存在同一路径

    案例:通过执行js,选中、取消选中、全选复选框。之后执行js判断是否全部选中,全选弹出“测试成功”,否则弹出“测试失败”

    Python代码

    #coding=utf-8
    from selenium import webdriver
    import time,os

    file_path = os.path.abspath('checkbox.html')
    print file_path
    driver = webdriver.Chrome()
    driver.get(file_path)
    #通过执行js,选中复选框
    driver.execute_script('var chk =document.getElementById("c1"); chk.checked="checked"')
    print driver.find_element_by_id("c1").is_selected()
    time.sleep(3)
    #通过执行js,取消选中复选框
    driver.execute_script('var chk =document.getElementById("c1"); chk.checked=""')
    print driver.find_element_by_id("c1").is_selected()
    time.sleep(3)
    #通过执行js,全选所有复选框,执行的js可以定义函数,通过调用函数全选
    driver.execute_script('var checkboxs=document.getElementsByTagName("input");
    for (var i=0;i<checkboxs.length;i++)
    { var e=checkboxs[i];
    if(e.type=="checkbox")
    {e.checked="checked";
    }
    };')
    time.sleep(3)
    #判断是否全选,全选弹出“测试成功”,未全选弹出“测试失败”
    driver.execute_script('function checkall()
    { var result=true;
    var checkboxs=document.getElementsByTagName("input");
    for (var i=0;i<checkboxs.length;i++)
    { var e=checkboxs[i];
    if (e.type=="checkbox")
    {if (!e.checked)
    { result=false;
    continue;
    }
    }
    }
    if(result)
    { alert("测试成功");
    }
    else
    { alert("测试失败");
    }
    }
    checkall()')

    HTML代码:保存为checkbox.html

    <html>
    <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>Checkbox</title>
    <script type="text/javascript" async="
    " src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"
    rel="stylesheet" />
    <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
    </head>
    <body>
    <h3>checkbox</h3>
    <div class="well">
    <form class="form-horizontal">
    <div class="control-group">
    <label class="control-label" for="c1">checkbox1</label>
    <div class="controls">
    <input type="checkbox" id="c1" />
    </div>
    </div>
    <div class="control-group">
    <label class="control-label" for="c2">checkbox2</label>
    <div class="controls">
    <input type="checkbox" id="c2" />
    </div>
    </div>
    <div class="control-group">
    <label class="control-label" for="c3">checkbox3</label>
    <div class="controls">
    <input type="checkbox" id="c3" />
    </div>
    </div>
    <input type="text" id="in1" value="干扰输入框" />
    </form>
    </div>
    </body>
    <!-- <script>
    function SelectAll() {
    var checkboxs=document.getElementsByTagName("input");
    for (var i=0;i<checkboxs.length;i++) {
    var e=checkboxs[i];
    if(e.type=='checkbox'){
    e.checked="checked";
    }
    }
    }
    SelectAll()
    function checkall()
    {
    var checkboxs=document.getElementsByTagName("input");
    console.log(checkboxs);
    var result=true;
    for (var i=0;i<checkboxs.length;i++)
    {
    var e=checkboxs[i];

    if(e.type=='checkbox'){
    if (!e.checked)
    {
    console.log(e.checked);
    result=false;
    continue;
    }
    }
    }
    console.log(result);
    if(result)
    {
    alert("测试成功");
    }
    else{
    alert("测试失败");
    }
    }
    checkall()
    </script> -->
    </script>
    </html>

     

  • 相关阅读:
    2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】
    JDBC连接MySQL数据库
    MySQL实习训练1
    I Hate It
    Just a Hook
    Mayor's posters
    asd的甩锅计划
    程序员的恋情
    Radical and array
    Highways
  • 原文地址:https://www.cnblogs.com/yan-xiang/p/6618187.html
Copyright © 2011-2022 走看看