Selenium self-summary 2_ elements Basic operations

Source: Internet
Author: User

For the operation of the basic elements of selenium, on their own understanding to do a basic introduction, this direct on the code, for a page how to write some basic operation script, hope to have some help for beginners, but also hope that through these summaries to let oneself have some clear understanding and understanding:

Demo file: Http://pan.baidu.com/s/1sjECS5BDemo's sample diagram:

Java Code:
ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Set;ImportJava.util.concurrent.TimeUnit;ImportOrg.openqa.selenium.Alert;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.JavascriptExecutor;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.chrome.ChromeDriver;Importorg.openqa.selenium.interactions.Actions;Importorg.openqa.selenium.support.ui.ExpectedCondition;ImportOrg.openqa.selenium.support.ui.Select;Importorg.openqa.selenium.support.ui.WebDriverWait; Public classbasicuiexercise {webdriver driver;  Public voidOpenbrowser () {System.setproperty ("Webdriver.chrome.driver",                "E:\\javacodeworkspace\\jproject0610\\chromedriver\\chromedriver.exe"); Driver=NewChromedriver (); }    //Exit all browsers     Public voidQuitbrowser () {driver.quit (); }    //exit the current single browser     Public voidCloseBrowser () {driver.close (); }     Public voidgototarget (String url) {driver.navigate (). to (URL);        Driver.navigate (). Refresh (); //Driver.get (URL);    }     Public voidBacktotarget () {driver.navigate (). back (); }     Public voidWaitTime () {driver.manage (). Timeouts (). Implicitlywait (10, Timeunit.seconds); }     Public voidMaxwindow () {driver.manage (). window (). Maximize (); }    //Drag the page scroll bar to the lowest part     Public voidExecutejs (String script) {//String js = "var q=document.documentelement.scrolltop=10000";Javascriptexecutor js =(javascriptexecutor) driver;    Js.executescript (script); }     Public voidtestinput (String value) {webelement element=driver.findelement (by. XPath (".//*[@id = ' input ']/input")); //element.clear ();Element.sendkeys (value); String Inputvalue= Element.getattribute ("Value");    System.out.println (Inputvalue); }     Public voidTestclicklink ()throwsinterruptedexception {webelement element= Driver.findelement (By.classname ("Baidu")); System.out.println ("The Link is:" + element.getattribute ("href"));        Element.click (); System.out.println ("Current page Title is:" +driver.gettitle ()); Thread.Sleep (3000); }     Public voidTestselect (intindex) {webelement element= Driver.findelement (By.name ("select")); Select Select=NewSelect (Element);        Select.selectbyindex (index); String SelectText=select.getfirstselectedoption (). GetText (); System.out.println ("CURRETN Select is:" +SelectText); }     Public voidTestradiobox (intindex) {List<WebElement> elements = driver.findelements (By.name ("Identity"))); webelement element=Elements.get (index);        Element.click (); BooleanFlag =element.isselected ();        GetClass (); System.out.println ("The Radiobox is Select:" +flag); }     Public voidTestmultiplebox () {List<WebElement> elements =driver.findelements (by. XPath (".//*[@id = ' checkbox ']/input"));  for(webelement element:elements) {Element.click (); BooleanFlag =element.isselected (); System.out.println ("The CheckBox is select:" +flag); }    }     Public voidtestbtn () {webelement element= Driver.findelement (By.classname ("button"));        Element.click (); String Btntext= Element.getattribute ("Value");    System.out.println (Btntext); }     Public voidTestalert ()throwsinterruptedexception {webelement element=driver.findelement (by. XPath (".//*[@id = ' alert ']/input"));        Element.click (); Thread.Sleep (1000); Alert Alert=Driver.switchto (). alert (); String Alerttext=Alert.gettext (); System.out.println ("ALert Text is:" +alerttext);    Alert.accept (); }    //Selenium is not recognized Windows dialog box, so in the process of uploading files depending on the browser, you need to use the AutoIt tool to get the Windows dialog box to implement the upload function     Public voidtestuploadfile (String filePath) {webelement element= Driver.findelement (By.id ("Load"));    Element.sendkeys (FilePath); }     Public voidTestopnenewwindow () {webelement element= Driver.findelement (By.xpath (".//*[@id = ' Open ']/a"));        Element.click (); String Currenthandle=Driver.getwindowhandle (); Set<String> handles =Driver.getwindowhandles (); Iterator<String> iterators =Handles.iterator ();  while(Iterators.hasnext ()) {String handle=Iterators.next (); if(!(Currenthandle.equals (handle)))                {Driver.switchto (). window (handle); System.out.println ("Testopnenewwindow Current Web Title:" +driver.gettitle ());  This. CloseBrowser ();    }} driver.switchto (). window (Currenthandle); }    //move the mouse to the btn below, there will be a corresponding information display     Public voidtestaction () {webelement element= Driver.findelement (By.classname ("Over")); Actions Actions=NewActions (driver);        Actions.movetoelement (Element). Build (). Perform (); String ActionText= Driver.findelement (By.id ("Over") . GetText (); System.out.println ("The Action Text is:" +ActionText); }    //testaction Popup information to be disposed of first, or testelementdisplayedwait unable to locate the current page, the focus point is still on the testaction above the information above     Public voidTestactionjavascript () {javascriptexecutor js2=(javascriptexecutor) driver; Js2.executescript ("Alert (' Hello Selenium Web test! ')"); Alert Alert=Driver.switchto (). alert (); String text=Alert.gettext ();        System.out.println (text);    Alert.accept (); }     Public voidtestelementdisplayedwait () {webelement element= Driver.findelement (By.classname ("Wait"));        Element.click (); webdriverwait wait=NewWebdriverwait (Driver, 10); Wait.until (NewExpectedcondition<boolean>() {@Override PublicBoolean Apply (Webdriver driver2) {returnDriver2.findelement (By.classname ("Red") . isdisplayed ();        }        }); System.out.println ("The context is:" + driver.findelement (By.classname ("Red") . GetText ()); }     Public Static voidMain (string[] args)throwsinterruptedexception {basicuiexercise Bue=Newbasicuiexercise ();        Bue.openbrowser ();        Bue.maxwindow ();        Bue.waittime (); Bue.gototarget ("E:\\javacodeworkspace\\jproject0610\\webaddress\\demo2.html"); Bue.testinput ("Test webdriver Study");        Bue.testclicklink ();        Bue.backtotarget (); Bue.testselect (4); Bue.testradiobox (5);        Bue.testmultiplebox ();        BUE.TESTBTN ();        Bue.testalert (); Bue.executejs ("Var q=document.documentelement.scrolltop=10000"); Bue.testuploadfile ("E:\\javacodeworkspace\\jproject0610\\webaddress\\demo2.html");        Bue.testopnenewwindow ();        Bue.testaction ();        Bue.testactionjavascript ();        Bue.testelementdisplayedwait (); Thread.Sleep (1000);    Bue.quitbrowser (); }}

Selenium self-summary 2_ elements Basic operations

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.