首页 > 其他 > 详细

Selenium 打开百度,简单代码

时间:2017-06-02 14:32:11      阅读:408      评论:0      收藏:0      [点我收藏+]

火狐浏览器

 

package com.selenium.Glen;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.*;

public class firefox {

    private static WebDriver driver;

    public static void main(String[] args) {
        
        //如果火狐浏览器没有默认安装在C盘,需要制定其路径
        System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
        WebDriver driver = new FirefoxDriver();
       driver.get("http://www.baidu.com/");
                
        driver.manage().window().maximize();
        
        WebElement txtbox = driver.findElement(By.name("wd"));
        txtbox.sendKeys("Glen");
        
        WebElement btn = driver.findElement(By.id("su"));
        btn.click();
        
        driver.close();

    }

}

谷歌浏览器

 

package com.selenium.Glen;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.*;

public class chromedriver {

    private static WebDriver driver;

    public static void main(String[] args) {
        
        //如果火狐浏览器没有默认安装在C盘,需要制定其路径
        //System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
      //  WebDriver driver = new FirefoxDriver();
      
            System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
         WebDriver webDriver = new ChromeDriver();
          driver.get("http://www.baidu.com");
         driver.manage().window().maximize();
          WebElement txtbox = driver.findElement(By.name("wd"));
        txtbox.sendKeys("Glen");
        
        WebElement btn = driver.findElement(By.id("su"));
        btn.click();
         driver.close();

    }

}

 

Selenium 打开百度,简单代码

原文:http://www.cnblogs.com/HenryLi03/p/6932999.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!