Selenium Webdriver Learning-by ID, name locator, input, search, close action

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.