"Turn" selenium position and toggle frame

Source: Internet
Author: User
Tags xpath

Reprinted from: http://www.voidcn.com/blog/huilan_same/article/p-6155896.html

A lot of people in the use of Selenium positioning page elements will encounter the problem of positioning, obviously the element is there, with Firebug can also be seen, is not located, this situation is likely to be frame in mischief (one of the reasons, another day specifically said the positioning of elements, Some of the possible causes and ways of handling).

Frame tag has frameset, frame, iframe three kinds, frameset with other ordinary label no difference, will not affect the normal positioning, and frame and IFRAME for selenium positioning is the same, Selenium has a set of methods to manipulate the frame.

1. How to cut into frame (Switch_to.frame ())

Selenium provides the Switch_to.frame () method to toggle the frame

Switch_to. FRAME (Reference)

Have to mention Switch_to_frame (), a lot of people in this writing will find that this sentence is marked with a strikethrough, because this method has been out, and then very likely will not support, the proposed wording is switch_to.frame ()

Reference is an incoming parameter used to locate the frame, which can pass in the ID, name, index, and selenium webelement objects, assuming the following HTML code index.html:

<Html Lang="EN"><Head><Title>frametest</Title></Head><Body><Iframe Src="A.html" Id= "frame1"  name= "MyFrame" ></ iframe></< Span class= "Hljs-title" >body></ html>            

To locate the IFRAME and cut it in, you can use the following code:

From selenium import webdriverdriver = Webdriver. Firefox () driver. switch_to. FRAME (0)#1.Use the index of frame to locate the first one is0# Driver.switch_to.frame ("Frame1")#2.Use ID to locate# driver.switch_to.frame (  "MyFrame" ) # 3.  use name to locate # Driver.switch_to.frame (Driver.find_element_by_tag_name (  "iframe" # 4.   with Webelement object         

Usually the ID and name are used to solve most problems. But sometimes frame does not have these two properties, you can use index and webelement to locate:

    • Index starting from 0, the incoming integer parameter is determined to use index positioning, the incoming str parameter is determined to use id/name positioning
    • Webelement object, which is the object obtained by using the Find_element series method, we can locate the frame object with Tag_name, XPath, etc.

Give me a chestnut:

<iframesrc="myframetest.html" />    

Use XPath to locate and pass in the Webelement object:

Driver. switch_to. FRAME (Driverfind_element_by_xpath ("//iframe[contains (@src, ' MyFrame ')]") 
2. Cut back the main document from the frame (switch_to.default_content ())

After we cut into the frame, we can't continue to manipulate the elements of the main document, and if we want to manipulate the contents of the main document, we need to cut back to the main document.

Driver. switch_to. Default_content () 
3. Operation of nested FRAME (Switch_to.parent_frame ())

Sometimes we encounter nested frame, as follows:

<Html><Iframe Id= "frame1" > < Span class= "Hljs-tag" ><iframe id= "frame2" /> </iframe> </html>              

1. Cut from the main document to frame2 and cut in layers

Driver. switch_to.frame ("frame1") driver. switch_to. FRAME ("frame2")    

2. From frame2 back to frame1, here Selenium provides us with a way to cut back from the child frame to the parent frame without having to cut back into the main document.

Driver. switch_to. Parent_frame ()  # No effect if it is currently a main document  

With Parent_frame () this is the equivalent of a fallback method, we can switch to different frame at will, random jump to jump.

So as long as the use of the following three methods, encounter frame minutes to take care of:

Driver. switch_to.frame (Reference) driver switch_to. Parent_frame()driver. switch_to. Default_content ()     


"Turn" selenium position and toggle frame

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.