Selenium Webdriver Learning---Three wait time methods: Explicit wait, implicit wait, forced wait
This example includes window maximization, refresh, switch to the specified window, back, forward, get the current window URL and other operations;
Import Java.util.Set;
Import Java.util.concurrent.TimeUnit;
Import Org.jsoup.Jsoup;
Import org.jsoup.nodes.Document;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import Org.openqa.selenium.chrome.ChromeDriver;
Import Org.openqa.selenium.phantomjs.PhantomJSDriver;
Import org.openqa.selenium.support.ui.ExpectedCondition;
Import org.openqa.selenium.support.ui.ExpectedConditions;
Import org.openqa.selenium.support.ui.Wait;
Import org.openqa.selenium.support.ui.WebDriverWait;
/**
* Method 3, implicit wait, implicit wait is equivalent to setting the global wait, when the element is positioned, the time-out is set for all elements.
* Implicit wait allows Webdriver to poll the DOM at a specific time when looking for an element or element array,
* If element or array is not found immediately. The default setting is 0. Once set, this implicit wait takes effect throughout the life cycle of the Webdriver object instance. Once.
* ********
* Method 1, force wait, fixed sleep time settings, Java Thread class provides sleep method sleep, import the package can be used,
* Execute until at this point, no matter what, wait 5 seconds before you proceed with the subsequent operation
* ********
* Method 2, explicit wait, in 0-10s to locate the element with the id "su", webdriverwait by default every 500ms is called once expectedcondition
* Until the location succeeds or the time cutoff, the return value of the expectedcondition is either true or not an empty object.
* until () will throw org.openqa.selenium.TimeoutException if no successful element is positioned within the specified time. */
public class ysftest_20180726{
public static void Main (string[] args) throws interruptedexception{
Webelement search = null;
System.setproperty ("Webdriver.chrome.driver", "C:/Program Files (x86)/google/chrome/application/chromedriver.exe" );
Webdriver Driver = new Chromedriver ();
Method 3, the implicit Wait, the Implicitlywait () method is smarter than the sleep () method, The sleep () method can only wait at a fixed time, and implicitlywait () can wait in a time range called implicit wait
Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);
Driver.get ("https://www.baidu.com/");
Window maximization
Driver.manage (). window (). Maximize ();
Locate the box you are searching for and enter the movie
Driver.findelement (By.cssselector ("#kw")). SendKeys ("film");
Method 1, force wait, fixed sleep time setting, sleep () method in milliseconds, this class is set to 5s wait
Thread.Sleep (5000);
Positioning Baidu Button
Webelement SearchButton = driver.findelement (By.cssselector ("#su"));
Click Baidu for a moment
Searchbutton.submit ();
Get the current window URL
String ParentURL = Driver.getcurrenturl ();
System.out.println ("Currenturl:" +parenturl);
Get Search Navigate Window
Driver.navigate (). to ("Https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=navigate");
Get the current window URL
String parentUrl2 = Driver.getcurrenturl ();
System.out.println ("Currenturl:" +parenturl2);
Refresh Page
Driver.navigate (). Refresh ();
Back to Baidu Home
Driver.navigate (). back ();
Method 2, an explicit wait, waits within a specified time to occur within a range of 10 seconds. The Red_box element is executed down, and if it doesn't appear after 10 seconds, jump out.
webelement element = (new webdriverwait (Driver, ten)). Until (expectedconditions.presenceofelementlocated ("su")) );
Advance to search Navigate window
Driver.navigate (). Forward ();
Driver.close ();
}
}
Note that if the content that is explicitly waiting for the search does not exist, it will run out of exception;
(Java) Selenium Webdriver Learning---Three wait time methods: Explicit wait, implicit wait, forced wait