package jkcs; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.NoSuchElementException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class jdcs { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); //设置安装路径,防止系统找不到 WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS); driver.get("http://www.baidu.com"); driver.manage().window().maximize(); try { SimpleDateFormat format = new SimpleDateFormat("HH-mm-ss-SSS"); String time = format.format(Calendar.getInstance().getTime()); System.out.println("开始的时间: " + time); driver.findElement(By.id("kw22")).sendKeys("selenium"); } catch (NoSuchElementException e) { System.out.println("没有找到元素"); e.printStackTrace(); } finally { SimpleDateFormat format2 = new SimpleDateFormat("HH-mm-ss-SSS"); String time2 = format2.format(Calendar.getInstance().getTime()); System.out.println("结束的时间: " + time2); driver.quit(); } } }
执行结果: