Automated Testing basics-Selenium iframe problem locating, basics-selenium
Sometimes we find a phenomenon on the way to positioning. The elements are there, but we cannot determine the location. At this time, we need the particularity of the specific webpage. This section describes how to handle iframe. I. Differences between frame and iframe: The functions of Frame and Iframe are basically the same, but Iframe is more flexible than Frame. Frame is the framework of the entire page. iframe is an embedded webpage element. It can also be said that the embedded framework Iframe mark is also called a floating frame mark, you can use it to embed an HTML document into an HTML file for display. The biggest difference between it and Frame tag is that the content embedded in the <Iframe> </Iframe> In the webpage is a whole with the whole page, the content contained in <Frame> </Frame> is an independent entity and can be displayed independently. In addition, the application Iframe can display the same content multiple times on the same page without repeating the content code. 2. NetEase mailbox (163 mailbox) is used for Logon as an example. Netease's mailbox is the most frequently used iframe. We often use Netease's mailbox for Logon exercises, the result shows that the entire mailbox cannot be set. Next we will resolve the 163 email logon step by step. Step 1: Open the login page. The first step of each test case is to open the browser and open the required webpage.
1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3 from selenium import webdriver4 import time5 url = 'http://mail.163.com/'6 browser = webdriver.Firefox()7 browser.get(url)8 time.sleep(3)
Step 2: positioning element: Let's first look at the difference between this and our common web page through firebug. We take Baidu as an example. Through comparison, we found that Baidu's search box is directly on the Top Window, the input box of the 163 mailbox is on iframe # x-URS-iframe, Which is iframe. How can we locate it? First, we need to switch to iframe. here we need to use the selenium method, and use the switch_to_frame method to switch. Here there is the id attribute, and you can use the id to locate the switch directly;
1 browser.switch_to.frame('x-URS-iframe')
Second, find the username, password, and logon button;
1 browser.find_element_by_name('email').send_keys('cf563540134')2 browser.find_element_by_name('password').send_keys('xxx')3 browser.find_element_by_id('dologin').click()
Finally, return to the main interface and release iframe;
1 browser.switch_to_default.content()
Step 3: How to Determine the iframe id?
1. here, iframe switching supports the id and name methods by default. Of course, in actual situations, if the id and name attributes are not empty, you must first locate iframe; 2. the positioning element is also applicable to the previous eight methods. Here, I can locate the element first through the tag to achieve the same effect.
1 iframe = browser.find_element_by_tag_name('iframe')2 browser.switch_to.frame(iframe)
Step 4: How to quickly determine whether a webpage has an iframe. Here we provide a simple method. Of course, it may not be very accurate, but it is the fastest and easiest. When we press F12, a firebug occurs, check whether Top Window or iframe # xxx is displayed in the upper left corner. If the Top Window appears, it indicates that there is no iframe and we can locate it normally; if the Top Window appears, iframe/frame. If iframe/frame exists, you need to switch to iframe/frame to locate it. Written at the end of the article: these are all compiled into a single word. Originality is not enough. There may be a lot of similar materials, the purpose of this article is to encourage you to develop good habits on the learning road. Therefore, please indicate the source for reprinting. Thank you!