Use the following example to write a use case
switch</title><link href= "http://cdn.bootcss.com/bootstrap/3.3.0/css/ Bootstrap.min.css " rel=" stylesheet "/>class=" Row-fluid " > class= "Span10 well" > script src= "Http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js" ></ Script>
Code:
PackageCom.test.frame;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.JavascriptExecutor;Importorg.openqa.selenium.NoSuchElementException;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.firefox.FirefoxDriver;Importorg.openqa.selenium.support.ui.ExpectedConditions; Public classFrameswitch { Public Static voidMain (string[] args) {Webdriver driver=NewFirefoxdriver (); Driver.get ("File:///D:/10-selenium/workspace/SeleniumTest/src/com/test/frame/frame.html"); Driver.manage (). window (). Maximize (); //use ID to switchWaitTime (2000); Driver.switchto (). FRAME ("If"); WaitTime (2000); if(Driver.findelement (By.xpath ("//*[@id = ' kw ']") . isdisplayed ()) {driver.findelement (By.xpath ("//*[@id = ' kw ')"). SendKeys ("switch successfully"); //Switch to Parent frameDriver.switchto (). Parentframe (); Try{driver.findelement (By.xpath ("//*[@id = ' kw ']")); } Catch(nosuchelementexception e) {String js= "alert (\" switch to parent\ ")"; ((Javascriptexecutor) driver). Executescript (JS); WaitTime (3000); Driver.switchto (). alert (). dismiss (); } } //use name to toggleWaitTime (2000); Driver.switchto (). FRAME ("NF"); WaitTime (2000); if(Driver.findelement (By.xpath ("//*[@id = ' kw ']") . isdisplayed ()) {driver.findelement (By.xpath ("//*[@id = ' kw ')"). SendKeys ("switch successfully"); Driver.switchto (). Parentframe (); Try{driver.findelement (By.xpath ("//*[@id = ' kw ']")); } Catch(nosuchelementexception e) {String js= "alert (\" switch to parent\ ")"; ((Javascriptexecutor) driver). Executescript (JS); WaitTime (3000); Driver.switchto (). alert (). dismiss (); } } //use positioning elements to switch-xpathWaitTime (2000); Driver.switchto (). FRAME (Driver.findelement (By.xpath ("//iframe[@id = ' if ']"))); WaitTime (2000); if(Driver.findelement (By.xpath ("//*[@id = ' kw ']") . isdisplayed ()) {driver.findelement (By.xpath ("//*[@id = ' kw ')"). SendKeys ("switch successfully"); Driver.switchto (). Parentframe (); Try{driver.findelement (By.xpath ("//*[@id = ' kw ']")); } Catch(nosuchelementexception e) {String js= "alert (\" switch to parent\ ")"; ((Javascriptexecutor) driver). Executescript (JS); WaitTime (3000); Driver.switchto (). alert (). dismiss (); } } //use positioning elements to switch-cssWaitTime (2000); Driver.switchto (). FRAME (Driver.findelement (By.cssselector ("#if"))); WaitTime (2000); if(Driver.findelement (By.xpath ("//*[@id = ' kw ']") . isdisplayed ()) {driver.findelement (By.xpath ("//*[@id = ' kw ')"). SendKeys ("switch successfully"); Driver.switchto (). Parentframe (); Try{driver.findelement (By.xpath ("//*[@id = ' kw ']")); } Catch(nosuchelementexception e) {String js= "alert (\" switch to parent\ ")"; ((Javascriptexecutor) driver). Executescript (JS); WaitTime (3000); Driver.switchto (). alert (). dismiss (); Driver.quit (); } } } Static Public voidWaitTime (intTime ) { Try{thread.sleep (time); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}Selenium Test (Java)-Multi-form switch (12)