selenium webdriver学习--通过id、name定位,输入内容,搜索,关闭操作

selenium webdriver学习--通过id、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{
     //加载驱动器
  System.setProperty("webdriver.chrome.driver","C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");

//通过id查找,以百度为例

  //打开浏览器
  WebDriver driver = new ChromeDriver();
  //打开网站
  driver.get("https://www.baidu.com/");
  //通过id定位所搜框
  WebElement searchBox = driver.findElement(By.id("kw"));
  //输入内容
  searchBox.sendKeys("电影");
  //定位百度一下按钮
  WebElement searchButton = driver.findElement(By.id("su"));
  //点击百度一下
  searchButton.submit();
  //等待5s
  Thread.sleep(5000);
  //页面关闭
  driver.close();


  //通过name查找,以豆瓣为例
  //打开浏览器
  WebDriver driver2 = new ChromeDriver();
  //打开网站
  driver2.get("https://www.douban.com/");
  //通过id定位所搜框
  WebElement searchBox2 = driver2.findElement(By.name("q"));
  //输入内容
  searchBox2.sendKeys("电影");
  //点击搜索
  searchBox2.submit();
  //等待5s
  Thread.sleep(5000);
  //页面关闭
  driver2.close();

    }
}

猜你喜欢

转载自www.cnblogs.com/xiao02fang/p/9366087.html