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

Source: Internet
Author: User

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

Knowledge points in this section:

Multi-layered frame or window positioning:

    • Switch_to_frame ()
    • Switch_to_window ()

Smart wait:

    • Implicitly_wait ()

For a modern web application, there are often frames or windowapplications, which poses a challenge to our positioning.

Sometimes we locate an element, the locator does not have a problem, but has not been able to locate, it is necessary to check whether this element is in a frame ,seelnium webdriver provides a switch_to_frame method, It can be easy to solve this problem.

Frame.html

<Html><Head><MetaHttp-equiv= "Content-type"Content= "Text/html;charset=utf-8"/><Title>frame</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(){});</Script></Head><Body><DivClass= "Row-fluid"><DivClass= "Span10 Well"><H3>frame</H3><IframeId= "F1"Src= "Inner.html"Width= "800", height= "600"></iframe>< Span style= "COLOR: #0000ff" ></div></div> </body><script src = "Http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js" Span style= "COLOR: #0000ff" >></script></html>    

Inner.html

<Html><Head><MetaHttp-equiv= "Content-type"Content= "Text/html;charset=utf-8"/><Title>inner</Title></Head><Body><DivClass= "Row-fluid"><DivClass= "Span6 Well"><H3>inner</H3><IframeId= "F2"Src= "Http://www.baidu.com"Width= "700"Height= "500"></iframe>< Span style= "COLOR: #0000ff" ><a href=" Javascript:alert (' watir-webdriver better thanselenium webdriver; ') " >click</a></div></div ></body></html>    

frame.html nested inner.html, two files and our script files are placed in the same directory:

Switch_to_frame ()

To manipulate the above page, the code is as follows:

#Coding=utf-8From seleniumImportWebdriverImportTimeImportOsbrowser =Webdriver. Firefox () File_path =‘file:///' + Os.path.abspath (‘Frame.html‘) Browser.get (File_path) browser.implicitly_wait (30)#First find the ifrome1 (id = f1) browser.switch_to_frame ("F1 ") # Then find the ifrome2 below it (ID =f2) browser.switch_to_frame ( "f2< Span style= "COLOR: #800000" > ") 

# Below you can manipulate elements browser.find_element_by_id (" kw" selenium ") browser.find_element _by_id ( "su" ). Click () time.sleep (3 Browser.quit ()

Driver.switch_to_window ()

It is possible to nest not frames, but windows, and true-to-window methods: Switch_to_window

Usage is the same as Switch_to_frame:

Driver.switch_to_window ("Windowname")

Implicitly_wait ()

Careful words will find the above example has browser.implicitly_wait (30), its usage should be more intelligent than time.sleep (), the latter can only choose a fixed time waiting, the former may be in a time range of intelligent waiting.

Document Explanation:

Selenium.webdriver.remote.webdriver.implicitly_wait (time_to_wait)

Implicitly waits for a vegan to be found or a command to complete; This method only needs to be called once per session

Time_to_wait: Wait Time

Usage:

Driver.implicitly_wait (30)

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

Learn more about selenium content:

Functional Test Automation Rollup

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

Related Article

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.