Selenium Webdriver Learning--by ID, name location, input content, search, close operation;
Open Google Browser, enter a different website, the search box positioning contains different elements (sometimes ID, sometimes name)
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import Org.openqa.selenium.chrome.ChromeDriver;
Import com.thoughtworks.selenium.Wait.WaitTimedOutException;
public class ysftest_20180719{
public static void Main (string[] args) throws interruptedexception{
Load Drive
System.setproperty ("Webdriver.chrome.driver", "C:/Program Files (x86)/google/chrome/application/chromedriver.exe" );
Search by ID, take Baidu as an example
//Open browser
webdriver driver = new Chromedriver ();
//Open the Web site
driver.get ("https://www.baidu.com/");
//Locate the box by ID
webelement searchbox = driver.findelement (by.id ("kw"));
//input
searchbox.sendkeys ("movie");
//positioning Baidu Button
webelement SearchButton = driver.findelement (by.id ("su"));
//Click Baidu for a Moment
searchbutton.submit ();
//wait for 5s
thread.sleep ();
//page Close
driver.close ();
Search by name, take watercress as an example
Open your browser
Webdriver Driver2 = new Chromedriver ();
Open Web site
Driver2.get ("https://www.douban.com/");
Locate the box by ID
Webelement SearchBox2 = driver2.findelement (By.name ("Q"));
Input content
Searchbox2.sendkeys ("film");
Click to search
Searchbox2.submit ();
Wait 5s
Thread.Sleep (5000);
Page Close
Driver2.close ();
}
}
Selenium Webdriver Learning-by ID, name locator, input, search, close action