Iwebdriver.switchto (). FRAME (Iwebelement frame)
If a page is an HTML element with only one head and one body, then using iwebdriver.findelement () to find any element in the page is not a problem. However, because the page <frame ... Use, so that an original HTML page can contain multiple sub-HTML pages, in this case, using iwebdrivr.findelement () to find an element of the page, if the element is an element of large HTML, then there is no problem. If the element is a <frame belonging to a child ... , the acquisition of the page element will fail. To succeed, you first need to figure out which frame the element belongs to, second, switch iwebdriver to the frame, and then use Iwebdriver.findelement () to find the element.
1. Get the frame that the page element belongs to and get its Name property.
2. Use Iwebdriver.findelements () to obtain all frames on this page, using Bytagname.
3. Loop through all frames to find the frame that matches the Name property.
4. Switch Iwebdriver to the frame to find the page element you want to get.
For example, my page elements are as follows:
In this page, to get the span element with the ID "TestCategory", it is useless to use the Iwebdriver.findelement (by.id ("TestCategory") directly, and this element cannot be found.
The correct code is as follows:
usingSe =Openqa.selenium;usingSIE =OpenQA.Selenium.IE; SIE. Internetexplorerdriver Driver=NewSIE. Internetexplorerdriver (); //All framesilist<se.iwebelement> frames = driver. Findelements (Se.By.TagName ("Frame")); Se.iwebelement Controlpanelframe=NULL; foreach(varFrameinchframes) { if(frame. GetAttribute ("name") =="Controlpanelframe") {Controlpanelframe=frame; Break; } } if(Controlpanelframe! =NULL) {driver. SwitchTo (). Frame (Controlpanelframe); } //find the Spane by ID in frame "controlpanelframe"Se.iwebelement spanelement = driver. Findelement (Se.By.Id ("TestCategory"));
Iwebdriver.switchto (). Window (String windowname)
Click a button on the page, then open a new window, switch the current iwebdriver focus to the new window, and use Iwebdriver.switchto (). Window (String windowname).
For example, I click the button to pop up a window called "Content Display", the way to switch focus to the new window is, first of all, to get the window name of the new window, you do not mistakenly think that page tile is window name Oh, If you use driver. SwitchTo (). Window ("Content display") is unable to find Windows name called "Content display", in fact, window name is a long list of numbers, similar to " 59790103-4e06-4433-97a9-b6e519a84fd0 ".
The way to correctly switch to Content Display is:
1. Get all the current windowhandles.
2. Loop through without a window to see if Window.title is "Content Display".
You see, the right code:
usingSe =Openqa.selenium;usingSIE =OpenQA.Selenium.IE; Public Static stringGotowindow (stringTitlerefSIE. Internetexplorerdriver driver) {driver. SwitchTo (). Defaultcontent (); //get all window handlesilist<string> Handlers =driver. Windowhandles; foreach(varWinhandlerinchhandlers) {driver. SwitchTo (). Window (Winhandler); if(Driver. Title = =title) { return "Success"; } Else{driver. SwitchTo (). Defaultcontent (); } } return "Window not Found"; }