WebDriver实现自动化打开IE中的google网页并实现搜索

时间:2021-12-12 22:51:13

WebDriver实现自动化打开IE中的google网页并实现搜索,需要导入WebDriver相关的jar包,设置好环境变量。

 

复制代码代码如下:


package com.feng.domain;

 

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class TestWebDriver {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  System.setProperty("webdriver.ie.driver", "C:\\testwebdriver\\IEDriverServer.exe");
  DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
  ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
  WebDriver driver = new InternetExplorerDriver(ieCapabilities);
  driver.get("http://www.google.com.hk/");
  try {
   Thread.sleep(6000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  // System.out.println("The current url is:" + driver.getCurrentUrl());
  WebElement element = driver.findElement(By.name("q"));
  element.sendKeys("days");
  WebElement elementBtn = driver.findElement(By.xpath("//input[@name='btnG']"));
  elementBtn.click();
  try {
   Thread.sleep(6000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  driver.close();
 }
}