Web UI Automated Testing - positioning element (c)

This article compiled element ndamentals --iframe frame elements positioned in.

A, iframe element positioning frame

  iframe is an inline frame Html page, if you can not locate an element in automated testing, it is likely because of the iframe element in the frame. First look at the following 3 sections of the code, and 2 are origin.html iframe page frame.

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>origin</title>
 6 </head>
 7 <body>
 8     <p id="origin_p">这是原始页面</p>
 9     <iframe src="iframe_1.html" id="parent"></iframe>
10     <br><br>
11     用户名:
12     <input type="text" id="username">
13 </body>
14 </html>
origin.html
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>iframe_1</title>
 6 </head>
 7 <body>
 8     <p id="parent_p">这是第1层iframe框架</p>
 9     <iframe src="iframe_2.html" id="son"></iframe>
10 </body>
11 </html>
iframe_1.html
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>iframe_2</title>
 6 </head>
 7 <body>
 8     <p id="son_p">这是第2层iframe框架</p>
 9 </body>
10 </html>
iframe_2.html

1. not nested iframe

  After the 9th line of code comments iframe_1.html, open origin.html page, you can see only one page origin iframe frame. Following picture, the red line is the portion of the first layer iframe nested frame.

figure 1

2. nested iframe 

  After iframe_1.html the 9th line code comments to cancel, open origin.html page, you can see there are two pages iframe framework of origin. Following picture, the red line is the portion of the first layer of nesting iframe frame, blue frame portion of the second layer is nested iframe frame.

figure 2

Second, examples

 1 import time
 2 
 3 from selenium import webdriver
 4 
 5 # 创建driver实例
 6 driver = webdriver.Chrome()
 7 # 窗口最大化
 8 driver.maximize_window()
 9 # 打开待测页面
10 driver.get('file:///python/selenium/origin.html')
11 # 隐式等待10s
12 driver.implicitly_wait(10)
13 # 定位原始页面中的p标签
14 origin_p = driver.find_element_by_id('origin_p')
15 # 打印原始页面中p标签的文本
16 print('原始页面p标签的内容:{}'.format(origin_p.text))
17 # 通过索引方式进入第一层iframe框架
18 driver.switch_to.frame(0)
19 # 定位iframe_1中的p标签
20 parent_p = driver.find_element_by_id('parent_p')
21 # 打印iframe_1中p标签的文本
22 print('第1层iframe框架中p标签的内容:{}'.format(parent_p.text))
23 # 通过id属性进入第二层iframe框架
24 driver.switch_to.frame('son')
25 # 定位iframe_2中的p标签
26 son_p = driver.find_element_by_id('son_p')
27 # 打印iframe_2中p标签的文本
28 print('第2层iframe框架中p标签的内容:{}'.format(son_p.text))
29 time.sleep(2)
30 # 跳转到原始页面
31 driver.switch_to.default_content()
32 # 定位原始页面中的input标签
33 user = driver.find_element_by_id('username')
34 # 在input标签中输入admin
35 user.send_keys ( ' ADMIN ' )
 36  # Wait 2S 
37 [ the time.sleep (2 )
 38 is  # Exit and close the browser driver 
39 driver.quit ()

Line 10 address to fill in part according to their actual address. Mainly the above code into the frame iframe method switch_to.frame () and two corresponding manner, i.e., id and indexing mode; switch_to.default_content out also illustrates a method of frame iframe ().

Results are as follows:

image 3

 

Reference: "Python automated test combat" - knows no boundaries

Guess you like

Origin www.cnblogs.com/cnblogs0/p/11234560.html