Today in writing a function of the script, found that some parts of the element is always unable to locate, looked at the next page of the HTML discovery element originally in the IFRAME, the use of the ID and Name property of the IFRAME with Switch_to_frame jump can be positioned, But found that some of the IFRAME does not have ID and name, suddenly do not know how to engage, Baidu, found the following article is good!
-------------------------the following transfers from: http://blog.sina.com.cn/s/blog_68f262210101mcxp.html
Recently in the webdriver of the web-level Automation test code writing, encountered a problem: the page has three tabs, click on a tab to open a page, the second tab to open the page to locate the operation, but always can't locate. Look at the code, found that the label opens a new page, nested in the IFRAME, so on the Internet to search the relevant positioning method.
For example, the existing HTML code is as follows:
- Main.html
- <</span>html>
- <</span>head>
- <</span>title>FrameTest</</span>title>
- </</span>head>
- <</span>body>
- <</span>div id = "ID1" >this is a div!</</span>div>
- <</span>iframe id = "frame1" frameborder= "0" scrolling= "no" style= "left:0;position:absolute;" src = " Frame.html "></</span>iframe>
- </</span>body>
- </</span>html>
- Frame.html
- <</span>html>
- <</span>head>
- <</span>title>this is a frame!</</span>title>
- </</span>head>
- <</span>body>
- <</span>div id = "Div1" >this is a div,too!</</span>div>
- <</span>label>input:</</span>label>
- <</span>input id = "INPUT1" ></</span>input>
- </</span>body>
- </</span>html>
1.iFrame case with ID or name
Enter the id= "FRAME1" frame and locate the id= "Div1" div and the input box id= "INPUT1".
Dr.switchto (). FRAME ("frame1");
Dr.findelement (By.id ("Div1"));
Dr.findelement (By.id ("INPUT1"))
2. If an IFRAME has no ID or name, the general case
Position the frame and select frame
Webelement frame=driver.findelement (By.xpath ("/html/body/div[2]/div[8]/div[2]/div[3]/div/div[2]/div/iframe"));
Driver.switchto (). frame (frame);
3. Jump out of the IFRAME
Jump out of frame, go to default content, reposition id= "Id1" div
Dr.switchto (). Defaultcontent ();
Dr.findelement (By.id ("Id1"))
Selenium element positioning within the IFRAME where the page element is positioned (including any ID, name)