首页 > 编程 > Java > 正文

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

2019-11-26 15:31:25
字体:
来源:转载
供稿:网友

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();
 }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表