首页 > 代码库 > Selenium 打开百度,简单代码

Selenium 打开百度,简单代码

火狐浏览器

 

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 打开百度,简单代码