1. Download the browser driver, using Chrome, Download http://chromedriver.storage.googleapis.com/index.html , press the corresponding browser to download a version number (not found with 106)
2.JDK before Eclipse has been installed, the new Maven project in Eclipse, adding a dependency selenium- java in the pom.xml.
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency>
3. New Test class, adding test code:
1 package com.java.selenium; 2 3 import java.util.Iterator; 4 import java.util.List; 5 import java.util.Set; 6 import java.util.concurrent.TimeUnit; 7 import org.openqa.selenium.Alert; 8 import org.openqa.selenium.By; 9 import org.openqa.selenium.Cookie; 10 import org.openqa.selenium.JavascriptExecutor; 11 import org.openqa.selenium.WebDriver; 12 import org.openqa.selenium.WebElement; 13 import org.openqa.selenium.chrome.ChromeDriver; 14 import org.openqa.selenium.interactions.Actions; 15 import org.openqa.selenium.support.ui.ExpectedConditions; 16 import org.openqa.selenium.support.ui.Select; 17 import org.openqa.selenium.support.ui.WebDriverWait; 18 19 20 public class SeleniumTest { 21 22 public static void main(String[] args) { 23 // TODO Auto-generated method stub 24 System.setProperty ( "webdriver.chrome.driver", "F: \\ WebDriver the Java Software \\ \\ \\ chromedriver.exe the Selenium"); // download chromedriver position 25 WebDriver Driver = new new ChromeDriver (); // instantiate an object ChromeDriver 26 String url = "http://www.baidu.com/" ; 27 driver.get (url); // open the specified website 28 29 // find the id of the element kw, the Selenium + enter the Java 30 // . driver.findElement (By.id ( "kW")) sendKeys ( "Java + Selenium"); 31 is // click su id of the element 32 // driver.findElement (By.id ( "su")) .click (); 33 34 // implicit wait for the page to load, once set up, this implicit wait will work throughout the life cycle WebDriver object instance, 35 // it is not subject to a certain element, is waiting for a global element, that is, the positioning element, all the elements need to wait for a page to load is completed, the 36 // before the next statement. If you exceed the set time exception is thrown. 37 [ // driver.manage () Timeouts () implicitlyWait (. 5, TimeUnit.SECONDS);.. 38 is 39 // display element specified wait wait wait time set recommendation 40 WebElement WE = new new WebDriverWait (Driver,. 5) .until (ExpectedConditions.presenceOfElementLocated (By.xpath ( "// * [@ the above mentioned id = \" qrcode \ "] / div / div [2]" ))); 41 42 // get the current browser page title and address 43 System. Out.println (driver.getTitle ()); 44 is System.out.println (driver.getCurrentUrl ()); 45 46 is // Get a single element 47 WebElement = driver.findElement Element (By.id ( "SU" )); 48 System.out.println ( "single element:" element.getAttribute + ( "class" )); 49 50 // Get a plurality of elements 51 is List <WebElement> ELIST = driver.findElements (By.tagName ( "INPUT" )); 52 is for (E WebElement: ELIST) { 53 is System.out.println ( "Get multiple elements:" + e.getAttribute ( "name" )); 54 is } 55 56 is // CSS level positioning elements 57 WebElement e = driver.findElement(By.cssSelector("div.qrcode-text")); 58 List<WebElement> list = e.findElements(By.tagName("p")); 59 for (WebElement e1 : list) { 60 System.out.println("css定位层级元素:"+e1.getAttribute("class")); 61 } 62 //xpath定位层级元素 63 WebElement e2 = driver.findElement(By.xpath("//*[@id=\"qrcode\"]/div/div[2]")); 64 List<WebElement> list2 = e.findElements(By.tagName("p")); 65 for (WebElement e1 : list) { 66 System.out.println ( "xpath level positioning elements:" + e1.getAttribute ( "class" )); 67 } 68 69 70 // get the current window 71 is String currentWindow = driver.getWindowHandle (); 72 the System.out. the println (currentWindow); 73 is 74 // get all of the windows 75 the Set <String> Handles = driver.getWindowHandles (); 76 for (S String: Handles) { 77 System.out.println (S); 78 } 79 80 / / traversal window 81 the Iterator <String> = Iteratorhandles.iterator (); 82 the while (iterator.hasNext ()) { 83 String Next Iterator.next = (); // here Iterator.next () can only be used once, or will cause bounds 84 IF (currentWindow.equals ( Next)) 85 Continue ; 86 // jump to the pop-up window 87 the WebDriver Driver2 = driver.switchTo () window (Next);. 88 driver2.getTitle (); 89 90 } 91 is 92 // execution of the JS script 93 String JSString = "Alert ( 'the Hello')" ; 94 the try { 95 ((JavascriptExecutor) Driver) .executeScript (JSString); 96 } the catch (Exception EX) { 97 // the TODO Auto-Generated Block the catch 98 ex.printStackTrace (); 99 } 100 101 // process popup 102 the Alert = Alert Driver .switchTo () Alert ();. 103 System.out.println (alert.getText ()); 104 // alert.dismiss (); // corresponds cancel 105 alert.accept (); // equivalent clicks confirmation 106 // alert.sendKeys ( "Testing"); //Input values, if possible 107 108 109 // treated drop-down list 110 / * the Select the Select new new SELECT = (driver.findElement (By.id ( "SELECT"))); 111 select.selectByIndex (. 1); 112 select.selectByValue ( "the Value"); 113 select.selectByVisibleText ( "the Text"); 114 // Get all options down box 115 List <WebElement> select.getOptions list3 = (); 1 16 for (WebElement WebElement: list3) { 117 webElement.click (); // click the drop-down box 118 } * / 119 120 // process Cookie 121 //添加一个cookie 122 Cookie cookie = new Cookie("sessionId","DF2334DFSDFOIFDFD221131"); 123 driver.manage().addCookie(cookie); 124 125 //获取cookie 126 Set<Cookie> cookieSet=driver.manage().getCookies(); 127 Iterator<Cookie> iteratorCookie = cookieSet.iterator(); 128 while(iteratorCookie.hasNext()) { 129 Cookie c= iteratorCookie.next(); 130 System.out.println(c.getName()); 131 System.out.println(c.getValue()); 132 } 133 134 // driver.manage () deleteAllCookies ();. Remove all cookie 135 // driver.manage () deleteCookie (cookie);. This object delete the cookie 136 // . Driver.manage () deleteCookieNamed ( "COOKIE" ); deleting the name of COOKIE Cookie 137 138 // mouse 139 the Actions Action = new new the Actions (Driver); 140 // move to double-click on an element plus perform () to take effect 141 is action.moveToElement (driver.findElement (By. XPath ( "// * [@ ID = \" SU \ "]" .))) a doubleClick () Perform ();. 142 // moved to right-click on an element 143 action.moveToElement (driver.findElement (By.cssSelector ( "the INPUT # kw" .))) contextClick () the perform ();. 144 145 driver.quit (); // exit the browser 146 147 } 148 149 }