Webdriver provides the Execute_script () interface to help us do this. In the actual test script, the following two scenarios are often encountered
- Perform a section of JS directly on the page
- Executes JS on an already positioned element
Js.html
<Html><Head><Metahttp-Equiv="Content-type"Content="Text/html;charset=utf-8"/><Title>Js</Title><ScriptType="Text/javascript"Async=""Src="Http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></Script><LinkHref="Http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"Rel="Stylesheet"/><ScriptType="Text/javascript">$(Document).Ready(function(){$(' #tooltip ').ToolTip({"Placement":"Right"});});</Script></Head><Body><H3>Js</H3><DivClass="Row-fluid"><DivClass="Span6 Well"><AId="ToolTip"Href="#"Data-Toggle="ToolTip"Title="Watir-webdriver Better than Selenium-webdriver">HoverToSeeToolTip</A><AClass= "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-*-FromSeleniumImportWebdriverFromTimeImportSleepImportOsIf' Http_proxy 'InchOs.Environ:DelOs.Environ[' Http_proxy ']Dr=Webdriver.Chrome()File_path=' file:///'+Os.Path.Abspath(' Js.html ')Dr.Get(File_path)# Execute JS directly on the pageDr.Execute_script(' $ ("#tooltip"). FadeOut (); ' ) sleep (1) span class= "C" ># executes Jsbutton = dr.find_element_by_class_name ( ' btn ' ) Dr . execute_script ( ' $ ( Arguments[0]). FadeOut () ' , button Span class= "P" > ) sleep (1) dr. Quit ()