The application of frame nesting often appears in Web applications, assuming a and b two frames on the page, where B is within a, then
Locating the contents of B requires first to A, then to B.
The Switch_to_frame method can switch the currently positioned body to the frame. How do you understand this sentence? We can get from frame
To understand the substance. The frame is actually embedded in another page, and Webdriver can only be recognized on one page at a time, so it needs
Use the Switch_to.frame method to get the embedded page in the frame and position the elements in that page.
In the following code, there is a frame with ID F1 in frame.html, and a frame with ID F2 is embedded in F1 and the frame loads
The homepage of Baidu.
Frame.html
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<title>frame</title>
<script type= "Text/javascript" async= "
"Src=" ></script> "Http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"
<link href= "Http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"
rel= "stylesheet"/>
<script type= "Text/javascript" >$ (document). Ready (function () {
});
</script>
<body>
<div class= "Row-fluid" >
<div class= "Span10 well" >
<iframe id= "F1" src= "inner.html" width= "" "Height=" ></iframe>
</div>
</div>
</body>
<script src= "Http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js" ></script>
Inner.html
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<title>inner</title>
<body>
<div class= "Row-fluid" >
<div class= "Span6 well" >
<iframe id= "F2" src= "http://www.baidu.com" width= "" "height=" >
</iframe>
</div>
</div>
</body>
frame.html nested inner.html, two files and our script files are placed in the same directory, opened through the browser, get
The following pages:
Figure 3.8
The following Switch_to_frame method is used to locate the elements within a frame:
#coding =utf-8
From selenium import Webdriver
Import time
Import OS
Driver = Webdriver. Firefox ()
File_path = ' file:///' + os.path.abspath (' frame.html ')
Driver.get (File_path)
Driver.implicitly_wait (30)
#先找到到 ifrome1 (id = f1)
Driver.switch_to_frame ("F1")
#再找到其下面的 ifrome2 (ID =f2)
Driver.switch_to_frame ("F2")
#下面就可以正常的操作元素了
driver.find_element_by_id ("kw"). Send_keys ("Selenium")
driver.find_element_by_id ("su"). Click ()
Time.sleep (3)
Driver.quit ()
Switch_to_frame the parameter problem. The official says name is acceptable, but the ID can also be found through experiments. So as long as frame
In the ID and name, it is easier to deal with them. If the frame does not have these two attributes, you can add it directly manually.
Go: The python webdriver API locates objects in a frame