1. Manipulating cookies
1 //Add a name = "name", value= "value" of the cookie2Cookie cookie =NewCookie ("name", "value")); 3 driver.manage (). Addcookie (cookie); 4 //get all the cookies under the current page and output their domain, name, value, valid date, and path5set<cookie> cookies =driver.manage (). GetCookies (); 6System.out.println (String.Format ("path", expiry, "Domain-Name,")); 7 for(Cookie c:cookies)8System.out.println (%s,%s,%s,%s,%s, String.Format), C.getdomain (), C.getname (), C.getvalue (), C.getexpiry (), C.getpath ()); 9}
Delete Cookies
1 // the first type of name through a cookie 2 driver.manage (). deletecookienamed ("CookieName"); 3 // second through Cookie object (cookie cookie) 4 5// Third Delete all 6
2. Wait for the page element to finish loading
1 //(1) Forced to wait for more abuse, will cause time waste or sleep time is not enough2Thread.Sleep (2000);3 4 //(2) Add cycle wait optimization5 LongStart =System.currenttimemillis ();6 while(true) {7Thread.Sleep (500); 8 if(Driver.gettitle (). IndexOf ("expectation")! =-1)9 Break; Ten if(System.currenttimemillis ()-Start >= 10000) One Break; A}
Stealth wait, set global element wait time-out. Implicit waiting is when you want to find the element, and this element does not appear immediately, tell Webdriver to query the DOM for a certain amount of time. The default value is 0, but after the setting, the time will work for the entire life cycle of the Webdriver object instance. However, there is a disadvantage, that is, the program will be in the set time to wait for the entire page to be loaded, before the next step, sometimes the individual JS loading is relatively slow to waste time.
1 // Set Stealth wait Time 10s 2
Explicit wait, using the Expectedconditions class in the self-contained method, you can make a trial wait for the judgment. The code will continue to execute backwards only if the condition satisfies the explicit wait, and the program throws an exception if it exceeds the set maximum explicit wait time. If both implicit and explicit waits are set, the maximum wait time is taken.
1 //set the dominant wait time, maximum wait of 10 seconds2webdriverwait wait =NewWebdriverwait (Driver, 10);3 //(1) whether the page element is available on the page and can be clicked4Webelement we =Wait.until (expectedconditions.elementtobeclickable (by locator));5 //(2) page element is selected6BooleanBoolean=Wait.until (expectedconditions.elementtobeselected (webelement Element));7 //(3) page elements exist in the page8Webelement we =Wait.until (expectedconditions.presenceofelementlocated (by locator));9 //(4) Whether a specific text is included in the page elementTenBooleanBoolean=Wait.until (Expectedconditions.texttobepresentinelement (webelement element, String text)); OneBooleanBoolean= Wait.until (Expectedconditions.texttobepresentinelement (by locator, String text));//deprecated A //(5) whether the page element value appears -BooleanBoolean=Wait.until (Expectedconditions.texttobepresentinelementvalue (by locator, String text)); -BooleanBoolean=Wait.until (Expectedconditions.texttobepresentinelementvalue (webelement element, String text)); the //(6) whether the title contains text -BooleanBoolean=Wait.until (Expectedconditions.titlecontains (String title)); - //(7) element visible -Webelement we =Wait.until (expectedconditions.visibilityofelementlocated (by locator)); + //(8) element not visible disappears -BooleanBoolean= Wait.until (expectedconditions.invisibilityofelementlocated (by locator));
3. Page
1 // 2 File screenshotfile = ((takesscreenshot) driver). Getscreenshotas (outputtype.file); 3 // Org.apache.commons.io.FileUtils.copyFile 4 New
4. Execute JS Script
1 // Create a Javascriptexecutor object 2 javascriptexecutor js =3//4 js.executescript (" document.getElementById (\ "Fromdate\"). Readonly=false "); 5 // 6 js.executescript (' document.getElementById (' id '). SetAttribute (' value ', ' 2018-05-10 '); ");
Java+selenium Webdriver Cookies, wait for advanced Operations (v)