Selenium Special window switch--iframe, pop-up window, dialog box _ dialog box

Source: Internet
Author: User
Tags tag name tagname
Window Category:
1.iframe
2. Window
3. dialog box
Common methods:
1.switchTo ()
2.getWindowHandles ()
Examples of IFRAME:
MU Course network path: After login homepage avatar-My course-wiki-create entries The
following figure is an iframe window

Task: Enter a paragraph of text under the iframe window: code:

Package com.wushuai.test;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;

Import Org.openqa.selenium.chrome.ChromeDriver;
    public class Windowtest {public Webdriver driver; Initialization of the Chrome browser driver public void Initdriver () {System.setproperty ("Webdriver.chrome.driver", "D:\\java\\chro
        Medriver-32\\chromedriver.exe ");
        Initialize the Chrome browser driver=new chromedriver ();
        Driver.get ("http://www.imooc.com/user/newlogin/from_url/");
    Screen maximization driver.manage (). window (). Maximize (); * * * Enter username and password and login * */public void InputBox () {////Enter account//Driver.findelement (BY.N
Ame ("email")). SendKeys ("123456@qq.com");  try {////Sleep two seconds//thread.sleep;//} catch (Interruptedexception e) {///
TODO auto-generated Catch block//E.printstacktrace (); ////Clear Account//Driver.findelement (By.name("email")).
Clear ();
String s=driver.findelement (by.name ("email")). GetAttribute ("placeholder");
Print information//SYSTEM.OUT.PRINTLN (s); /*//*//* Enter username and password and click login//*//* Here is mainly to lay the groundwork for personal setup//* * */Driver.fin
        Delement (by.name ("email")). SendKeys ("m13031155057@163.com");
        Driver.findelement (By.name ("password")). SendKeys ("123456");
        Driver.findelement (By.classname ("btn-red")). Click ();
        try {//Sleep two Seconds Thread.Sleep (2000);
        catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace (); }/* * Toggle Window Method * * */public void Iframetest () {//Enter the IFRAME window page driver.get ("http:/
        /www.imooc.com/wiki/create ");
        We must find the ID of our IFRAME webelement iframe=driver.findelement (by.id ("Ueditor_0"));
        Find and switch to this page Driver.switchto (). FRAME (IFRAME); When you switch to this page, locate the tag tag in the page and enter"Hello World", note that because there is only one body inside the body and the element tag//very little, so it is best to navigate to the label inside the Driver.findelement (By.tagname ("Body")). SendKeys ("he
    Llo world! ");} public static void Main (string[] args) {//TODO auto-generated method stub windowtest wt=new windowtest ()
        ;
        Wt.initdriver ();
        Wt.inputbox ();
    Wt.iframetest ();
 }

}
Toggle Window

Code:

Ackage Com.wushuai.test;
Import java.util.List;

Import Java.util.Set;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import Org.openqa.selenium.chrome.ChromeDriver;

Import org.openqa.selenium.interactions.Actions;
    public class WindowTest2 {public Webdriver driver;
    Public String WH; Initialize the Chrome browser driver and let the Chrome browser open the Web page public void Initdriver () {System.setproperty ("Webdriver.chrome.driv
        Er "," d:\\java\\chromedriver-32\\chromedriver.exe ");
        Initialize the Chrome browser driver=new chromedriver ();
        Driver.get ("http://www.imooc.com/");
    Screen maximization driver.manage (). window (). Maximize (); * * * Use action to perform left mouse click, right click, double click, hover operation * * * */public void Actiontest () {//Find our MU Net home page contains the previous paragraph development, backend development,
        Mobile development waits for a series of entries of the ClassName webelement login=driver.findelement (By.classname ("menucontent"));
     The list collection receives the previous paragraph development, the backend development, the Movement development entry, the attention uses is findelements not findelement, one more "s"   List<webelement> elements=login.findelements (By.classname ("item"));
        Creates an action object, and specifies the browser actions actions=new actions (driver);
        Click to operate the incoming parameter and use perform () to submit it for//actions.click (login). perform ();
        Double-click the action//actions.doubleclick (login). perform ();
        Right-click Operation//actions.contextclick (Login). perform ();
        Actions.movetoelement (). perform ();
        The first element in the list collection is the mouse hover actions.movetoelement (elements.get (0)). Perform ();
        Get the current Window Wh=driver.getwindowhandle ();
    Click the "HTML/CSS" entry contained in "Previous development" Driver.findelement (By.partiallinktext ("Html/css")). Click (); * * * * * * * * * * * * WindowHandle () {//* * * * * * * * * * * * * * * * * * * * */ 
        Lement: {"method": "Tag name", "selector": "Case"}//main or parent-child element error//driver.findelement (By.tagname ("case")). Click (); So we do the following code is still not passed, because we are operating here is the window value string type, rather than the previous similar item element value//Webelement Window=driver. Findelement (By.classname ("BD"));
List<webelement> tt=window.findelements (by.tagname ("Li"));

        Driver.switchto (). Window (Tt.get (2));
        * * * The following code is the correct */set<string> handles=driver.getwindowhandles ();

            for (String s:handles) {if (S.equals (WH)) {continue;
            } System.out.println (s);

        Driver.switchto (). window (s);
    } driver.findelement (By.linktext ("case")). Click ();
        public static void Main (string[] args) {WindowTest2 wt2=new WindowTest2 ();
        Wt2.initdriver ();
        Wt2.actiontest ();
    Wt2.windowhandle ();
 }

}
Summarize:
1. Notice the window of the IFrame, pop-up box, etc. application
2. Note the difference and application between the set<> of window class elements and common set list<>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.