8. 執行js

來源:互聯網
上載者:User

標籤:style   http   java   color   os   strong   

webdriver提供了execute_script()介面來協助我們完成這一工作。在實際的測試指令碼中,以下兩種情境是經常遇到的

  • 在頁面直接執行一段js
  • 在某個已經定位的元素的上執行js

 

 

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="watir-webdriver better than selenium-webdriver">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>

==============================================================================================================================================
js.python

# -*- coding: utf-8 -*- from selenium import webdriverfrom time import sleepimport osif ‘HTTP_PROXY‘in os.environ: del os.environ[‘HTTP_PROXY‘]dr = webdriver.Chrome()file_path = ‘file:///‘ + os.path.abspath(‘js.html‘)dr.get(file_path)# 在頁面上直接執行jsdr.execute_script(‘$("#tooltip").fadeOut();‘)sleep(1)# 在已經定位的元素上執行jsbutton = dr.find_element_by_class_name(‘btn‘)dr.execute_script(‘$(arguments[0]).fadeOut()‘, button)sleep(1)dr.quit()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.