轻松自动化---selenium-webdriver(python) (八)

发表于:2013-8-16 11:03

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:虫师    来源:51Testing软件测试网采编

  隐藏元素

  js.html

<html>
    <head>
      <meta http-equiv="content-type" content="text/html;charset=utf-8" />
      <title>js</title>    
      <script type="text/javascript" async="" src="http://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 type="text/javascript">
        $(document).ready(function(){
          $('#tooltip').tooltip({"placement": "right"});
        });
      </script>
    </head>

    <body>
      <h3>js</h3>
      <div class="row-fluid">
        <div class="span6 well">       
          <a id="tooltip" href="#" data-toggle="tooltip" title=" selenium-webdriver(python)">hover to see tooltip</a>
          <a class="btn">Button</a>
        </div>     
      </div>       
    </body>
    <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
  </html>

  (保持html文件与执行脚本在同一目录下)

  执行js一般有两种场景:

  一种是在页面上直接执行JS

  另一种是在某个已经定位的元素上执行JS

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

driver = webdriver.Firefox()
file_path =  'file:///' + os.path.abspath('js.html')
driver.get(file_path)

#######通过JS 隐藏选中的元素#########
#第一种方法:
driver.execute_script('$("#tooltip").fadeOut();')
time.sleep(5)

#第二种方法:
button = driver.find_element_by_class_name('btn')
driver.execute_script('$(arguments[0]).fadeOut()',button)
time.sleep(5)

driver.quit()

32/3<123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号