1. Get the element text
The GetText () method of the Webelement class returns the InnerText property of the element. So the element will be returned if it has a child node. As shown below
1 Public classGetText {2 @Test3 Public voidTestgettext () {4 //start driver, open the page under test5System.setproperty ("Webdriver.chrome.driver", "Chromedriver.exe");6Webdriver Driver =Newchromedriver ();7Driver.get ("http://map.baidu.com");8 9 //wait a while for the page to load and avoid problems with elements that cannot be foundTen Try{ OneThread.Sleep (2000); A}Catch(Exception e) { - - } the - //Get elements -Webelement message = Driver.findelement (By.classname ("Open-map")); - + //Get element text -String MessageText =Message.gettext (); + A //Verify that the text is "View short rent, job search, ordering meals, weather and more thematic maps >>" atAssert.assertequals (MessageText, "View short rent, job search, ordering, weather and more thematic maps >>"); - - //can be partially matched using the Java String API method -Assert.asserttrue (Messagetext.contains ("Job search"))); -Assert.asserttrue (Messagetext.startswith ("View short rent")); -Assert.asserttrue (Messagetext.endswith (">>")); in - //Close Driver to driver.quit (); + } -}
code Example
2. Get element attribute values
The GetAttribute () of the Webelement class returns the attribute value of the element
3. Get CSS property values for an element
The Getcssvalue () method of the Webelement class returns the Style property value of the element
4. Perform the mouse double-click on the element
The DoubleClick () method of the Actions class
5. Executing JavaScript code
Javascriptexecutor interface, can be arbitrary execution of JavaScript code, especially for some selenium webdriver currently not supported operations
((Javascriptexecutor) driver). Executescript (script);
6. Maximize the browser window
Driver.manage (). window (). Maximize ();
7. Working with Windows processes
Selenium Webdriver Java provides the Windowsutils class to interact with Windows systems. At the beginning of the test, we need to turn off some existing processes.
1 Public classKillwindows {2 3 @Test4 Public voidTest () {5 6 //Close the IE browser process7Windowsutils.trytokillbyname ("Iexplore.exe");8 9System.setproperty ("Webdriver.chrome.driver", "Chromedriver.exe");TenWebdriver Driver =Newchromedriver (); OneDriver.get ("http://www.baidu.com"); A - driver.quit (); - } the}
Sample Code
We can use the Trytokillbyname method to close any Windows process.
If the process does not exist, it throws an exception, but the test will continue to execute normally.
8. Read/Modify the values in the Windows registry
The Windowsutils class provides several ways to interact with the registry of the Windows operating system. If the test is an IE browser running on a Windows operating system, you may need to modify some of the settings in the IE registry. Using the Windowsutils class can be a convenient solution to this problem.
Depending on the return value data type, you can choose from the following methods:
Readstringregistryvalue ()
Readintergerregistryvalue ()
Readbooleanregistryvalue ()
Modify:
Writestringregistryvalue ()
Writeintergerregistryvalue ()
Writebooleanregistryvalue ()
[Selenium Webdriver Java] Common API