selenium在java中的使用

一、selenium(firefoxDriver/chromeDriver….)

资源链接:
官网:http://www.seleniumhq.org/download/maven.jsp
webDriver中文社区:http://www.webdriver.org/nav1/
是什么?:
Selenium是一个用于Web应用程序测试的工具;
后台代码模拟启动浏览器,访问页面等一些列操作。
应用场景?:
自动化测试、爬虫等。
如何构建?:
参考官网构建说明。

document.readyState几种状态及示例
http://www.runoob.com/jsref/prop-doc-readystate.html
- uninitialized - 还未开始载入
- loading - 载入中
- interactive - 已加载,文档与用户可以开始交互
- complete - 载入完成

demo:

package com.webdriver;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

/**
 * webdriverDemo
 * @author yisea
 *
 */
public class WebDriverTest {
    /**
     * 1.selenium 2.xx. 版本需要使用低版本的firefox浏览器,比如selenium2.45.0 对firefox 36可用(其他版本可自行尝试)
     * 2.selenium 3.0.0 需要额外驱动webdriver.firefox.marionette=path/geckodriver.exe
     * 所有版本必修保证浏览器启动exe路径能被加载(默认安装自动加载,否则需手动配置):
     * -Dwebdriver.firefox.bin="D:/Program Files/Mozilla Firefox 36/firefox.exe"
     * -Dwebdriver.firefox.marionette="D:/Program Files/Mozilla Firefox 36/geckodriver.exe"
     */

    private static WebDriver driver = null;

    public WebDriver getFireFoxDriver(){
        if(null == driver){
            driver = new FirefoxDriver(); 
        }
        System.out.println("Inite fireFoxDriver over!-------");
        return driver;
    }

    /**
     * 测试firefoxDriver
     */
    public void testFireFoxDriver(){
        WebDriver driver = getFireFoxDriver();
        driver.get("https://www.baidu.com/");
        /**
         * 返回页面加载状态
         *  document.readyState几种状态及示例
         *  - uninitialized - 还未开始载入
         *  - loading - 载入中
         *  - interactive - 已加载,文档与用户可以开始交互
         *  - complete - 载入完成
         *
         */
        String get_ready_state_js = "return document.readyState;";
        JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;

        String onload = jsExecutor.executeScript(get_ready_state_js).toString();
        if("complete".equals(onload)){
            // 执行js id=kw ; id=su
            String baidu_input_js = "$('#kw').attr('value','北京')";
            // 执行给百度输入框赋值js
            jsExecutor.executeScript(baidu_input_js);

            // 模拟点击-百度一下
            WebElement clickElement = driver.findElement(By.id("su"));
            Actions actions = new Actions(driver);
            actions.click(clickElement).build().perform();
        }else{
            System.out.println("fail to load page!----------");
        }

        try {
            // 让浏览器飞一会...
            System.out.println("Let browser fly ...");
            for(int i = 1; i < 11; i++){
                System.out.println(i + "s...");
                Thread.sleep(1000L);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 关闭webDriver
        driver.close();
        System.out.println("fireFoxDriver closed!------");
    }

    public static void main(String args[]){
        WebDriverTest test = new WebDriverTest();
        test.testFireFoxDriver();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值