Today, summarize how selenium operates the common elements of Web pages.
Mainly include:
- Upload
- Alter Dialog
- Prompt dialog
- Confirm Dialog
- SELECT list
- Radio Box
- Input box
- CheckBox
The test page is as follows:
View Code
Selenium's core skill is the identification and positioning of webelement.
There are eight methods of locating selenium
- By.id () Location by ID
- By.name () location by name
- By.xpath () Positioning by XPath
- By.classname () positioning via ClassName
- By.cssselector () positioning via CSS
- By.linktext () by LinkText
- By.tagname () by TagName
- By.partiallinktext () through the linktext part of the horse
Currently, using more is Cssselector and XPath, because a page in the ID name className tagName linktext etc easier to repeat is not easy to determine the unique
Here's a concrete example to show how to locate
Uploading files
In general, upload the page if it is input, like this, you can use SendKeys
First, use XPath to navigate to the element, then use SendKeys, SendKeys send the absolute path of the file you want to upload
1//td/input[@type = ' file ']
You can also use cssselector, more knowledge about XPath and Cssselector, and will do some topics later
1 td>input[type= ' file ']
How do you verify that the xptah/cssselector you wrote is correct?
There are several ways to do this:
- F12, using the browser's console, XPath uses $x () Function,css to use the $ () function
- Using third-party plug-ins Firebug
- Using the Selenium IDE
Alert dialog box
Three kinds of subdivision, alert,prompt,confirm
Selenium has the following methods:
Alert Alert =driver.switchto (). alert ();
1. Driver.switchto (). alert (); Get alert
2. alert.accept (); Click OK
3. Alert.dismiss (); Click Cancel
4. Alert.gettext (); Get Alert content
Select Menu
Select is also more common, selenium encapsulates the following methods
Create a Select
Webelement selector = driver.findelement (by.id ("selector")); Select select = New Select (selector);
There are three ways to choose option for select:
- Selectbyindex (int index) by index
- Selectbyvisibletext (String text) by matching to the visible character
- Selectbyvalue (String value) by matching the value in the tag
Webelement selector = driver.findelement (by.id ("selector")); Select select = New Select (selector); Select.selectbyindex (3); Select.selectbyvisibletext ("Peach"); Select.selectbyvalue ("Apple");
Input box
The input box is relatively simple, no more speaking
SendKeys () input content
Clear () Clear
Radio Box (Radiobox)
The radio box can have the get state, whether it is selected
Radiobox.isselected ();
Whether to enable
Radiobox.isenabled ()
Use the click Method to select
check box (checkbox)
check boxes and Radio boxes are basically the same, which is slightly
Hypertext links
Hyperlinks are more common and are usually labeled a
<a href= "http://www.cnblogs.com/tobecrazy/" >copyright to be Crazy </a>
General use of the click Method
Here we know that when the browser opens the hyperlink, if it is chrome, clicking the hyperlink while pressing CTRL will open the new tab, holding down shift will open a new window
Here is a single talk about a different window switch.
Selenium has two ways to get a window:
1. Driver.getwindowhandle (); Returns a string that gets the handle of the current window
2. Driver.getwindowhandles (); Returns the set<string>, gets all the Windows
If you want to switch between windows
Driver.switchto (). window (window);
Tips: How to scroll to the element you're targeting
Use Java Script here
Scroll to MyLink javascriptexecutor scroll = (javascriptexecutor) driver; Scroll.executescript ("Arguments[0].scrollintoview ();", MyLink);
Next is all the test code:
Package Com.packt.webdriver.chapter1;import Java.util.list;import Java.util.set;import Java.util.concurrent.timeunit;import Org.openqa.selenium.alert;import Org.openqa.selenium.by;import Org.openqa.selenium.javascriptexecutor;import Org.openqa.selenium.keys;import Org.openqa.selenium.WebDriver; Import Org.openqa.selenium.webelement;import Org.openqa.selenium.interactions.actions;import Org.openqa.selenium.support.ui.select;import Org.testng.assert;import Com.packt.webdriver.chapter3.DriverFactory ;/** * This method was for deal with the base web elements * * @author young * */public class Dealwithelements {public STA tic void Main (string[] args) throws Exception {String URL = "file://demo.html"; String chromdriver= "E:\\chromedriver.exe"; System.setproperty ("Webdriver.chrome.driver", chromdriver); Chromeoptions options = new Chromeoptions ();//Options.addextensions (New File ("")); Desiredcapabilities capabilities = Desiredcapabilities.chrome (); Capabilities.setcapability ("Chrome.switches", Arrays.aslist ("--start-maximized")); Options.addarguments ("--test-type", "--start-maximized"); Webdriver driver=new chromedriver (options); Driver.get (URL); Max Size the browser driver.manage (). window (). Maximize (); Driver.manage (). Timeouts (). Pageloadtimeout (Timeunit.seconds); Webelement upload = Driver.findelement (by. XPath ("//td/input[@type = ' file ']"); Upload.sendkeys ("c:/users/young/desktop/demo.html"); Assert.asserttrue (Upload.getattribute ("value"). Contains ("demo")); For alert webelement Clickonalert = Driver.findelement (by. XPath ("//td/input[@name = ' Alterbutton ') ")); Clickonalert.click (); Delay (2); Get alert Alert alert = Driver.switchto (). alert (); Assert.asserttrue (Alert.gettext (). Contains ("alert")); Click Alert OK alert.accept (); Delay (2); for prompt Webelement clickonprompt = driver.findelement (by. XPath ("//td/input[@name = ' Prompt Button '])); Clickonprompt.click (); Delay (2); Alert prompt = Driver.switchto (). alert (); Prompt.sendkeys ("I love Selenium"); Prompt.accept (); Delay (5); Alert afteraccept = Driver.switchto (). alert (); Assert.asserttrue (Afteraccept.gettext (). Contains ("I Love Selenium")); Click Alert OK afteraccept.accept (); Delay (2); For confirm webelement clickonconfirm = Driver.findelement (by. XPath ("//td/input[@name = ' Confirmbu Tton ']); Clickonconfirm.click (); Delay (2); Alert confirm = Driver.switchto (). alert (); Confirm.dismiss (); Delay (2); Alert Afterdismiss = Driver.switchto (). alert (); Assert.asserttrue (Afterdismiss.gettext (). Contains ("You pressed Cancel"); Delay (2); Afterdismiss.accept (); Driver.managE (). Timeouts (). implicitlywait (Timeunit.seconds); Webelement selector = driver.findelement (by.id ("selector")); Select select = New Select (selector); Select.selectbyindex (3); Select.selectbyvisibletext ("Peach"); Select.selectbyvalue ("Apple"); System.out.println (Select.getallselectedoptions (). toString ()); Delay (2); Webelement Showselectresult = Driver.findelement (by. Name ("Showselectresult")); Showselectresult.click (); Delay (2); Alert Yourselect = Driver.switchto (). alert (); Assert.asserttrue (Yourselect.gettext (). Contains ("Apple")); Delay (2); Yourselect.accept (); Input box Webelement editbox = Driver.findelement (by. XPath ("//td/input[@id = ' edit ']"); Editbox.sendkeys ("Selenium is good"); Webelement Submitbutton = Driver.findelement (by. XPath ("//input[@type = ' button ' and @name = ' submit ']"); Submitbutton.click (); Delay (2); Alert Submitalert = Driver.switchto (). alert (); Assert.assertequals (Submitalert.gettext (), "Selenium is good"); Submitalert.accept (); Delay (2); For radio Box webelement duradiobox = Driver.findelement (by. Cssselector ("Div#radio>input. Baidu ")); Webelement Aliradiobox = Driver.findelement (by. Cssselector ("Div#radio>input. Alibaba ")); Webelement Txradiobox = Driver.findelement (by. Cssselector ("Div#radio>input. Tencent ")); Webelement Miradiobox = Driver.findelement (by. Cssselector ("Div#radio>input. Mi ")); Delay (2); Assert.asserttrue (txradiobox.isselected ()); Assert.asserttrue (! Miradiobox.isenabled ()); Delay (2); Duradiobox.click (); Assert.asserttrue (duradiobox.isselected ()); Delay (2); Aliradiobox.click (); Assert.asserttrue (aliradiobox.isselected ()); Delay (2); For CHECKbox list<webelement> WebCheckBox = driver.findelements (by. XPath ("//input[@type = ' checkbox ']") ); for (Webelement E:webcheckbox) {E.click (); Assert.asserttrue (e.isselected ()); Delay (2); }//For links String Defaultwindow = Driver.getwindowhandle (); Webelement MyLink = Driver.findelement (by. LinkText ("Copyright Crazy")); Delay (3); Scroll to MyLink javascriptexecutor scroll = (javascriptexecutor) driver; Scroll.executescript ("Arguments[0].scrollintoview ();", MyLink); Open link in a new Windows press Shift-click Delay (2); actions = new actions (driver); Actions.keydown (Keys.shift). Click (MyLink). Perform (); Delay (3); set<string> currentwindows = Driver.getwindowhandles (); System.out.println (Currentwindows.size ()); for (String window:currentwindows) { if (!window.endswith (Defaultwindow)) {driver = Driver.switchto (). window (window); Driver.manage (). Timeouts (). Pageloadtimeout (Timeunit.seconds); Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); Break }} assert.asserttrue (Driver.getcurrenturl (). Contains ("Tobecrazy")); Delay (10); Driver.quit (); }/** * @author young * @param seconds */public static void delay (int seconds) {try { Thread.Sleep (seconds * 1000); } catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace (); } }}
Source: http://www.cnblogs.com/tobecrazy/p/4570494.html
Selenium summary, common methods and operation of page elements