Easy Automation---selenium-webdriver (python) (eight)

Source: Internet
Author: User

http://www.testclass.net/ Test Tutorial Network, professional Selenium learning website.

This section focuses on:

Invoking the JS method

    • Execute_script (script, *args)

synchronously executes javaScript in the current window/frame

Script: execution of JavaScript.

* Parameters: Applies to any JavaScript script.

Use:

Driver.execute_script(' Document.title ')

make the fast-seeding login user name input box red display :

#Coding=utf-8From seleniumImportWebdriverImportTimedriver =Webdriver. Firefox () Driver.get ("Http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fvod.kuaibo.com%2F%3Ft%3Dhome")#The input box for the user name is labeled Red js="var Q=document.getelementbyid (\ "user_name\"); q.style.border=\ "1px solid red\";"#Call JSdriver.execute_script (JS) time.sleep (3) driver.find_ element_by_id ( "user_name " username< Span style= "COLOR: #800000" > ") driver.find_element_by_id (  "user_pwd"). Send_keys ( Span style= "COLOR: #800000" > "password" ) driver.find_element_by_id (" dl_an_ Submit "). Click () time.sleep (3 ) driver.quit ()           

JS Explanation:

Q=document.getelementbyid (\ "User_name\")

The ID of element Q is user_name

Q.style.border=\ "1px solid red\

The style of element Q with a border of 1 pixels red

Hide elements

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= "Selenium-webdriver (python)">hover to see ToolTip</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>      

( keep the HTML file in the same directory as the execution script )

There are two common scenarios for executing JS:

    • One is to directly execute JS on the page
    • The other is to execute JS on an already positioned element.

#Coding=utf-8From seleniumImportWebdriverImportTime,osdriver =Webdriver. Firefox () File_path =‘file:///' + Os.path.abspath (‘Js.html‘) Driver.get (File_path)###### #通过JS Hide selected elements ######### # The first method: Driver.execute_script (  "$ (" #tooltip "). FadeOut (); ) time.sleep (5# The second method: Button = Driver.find_element_by_class_name ( "btn ") Driver.execute_script ( ' $ (Arguments[0]). FadeOut () ,button) time.sleep (5    

JS Explanation:

A Arguments object that is a special property of the calling object that is used to reference the Arguments object. arugments objects are like arrays.

The FadeOut () method uses a fade-out effect to hide the selected element, if the element is hidden.

ps: can see js  can do selenium  do not do things, but in what kind of automation can (or must) to js Help, I haven't met. But js can selenium It is beyond doubt to accomplish more powerful functions.

In addition, did not learn JS before, so the JS code is very strange. If you have time, you are advised to add this knowledge to your classmates. UI automation requires front-end technology.

--------------------------

Learn more about selenium content:

Functional Test Automation Rollup

Easy Automation---selenium-webdriver (python) (eight)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.