selenium实战:操作浏览器和进程

时间:2021-09-07
本文章向大家介绍selenium实战:操作浏览器和进程,主要包括selenium实战:操作浏览器和进程使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
package javaBase;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.os.WindowsUtils;
import org.openqa.selenium.support.ui.Select;
import java.io.File;
import java.io.IOException;


/**
 * java selenium 操作浏览器和进程 
 */
public class TestSetSeleniumServerJAR {
    public static void main(String[] args) {

        //谷歌浏览器
        WebDriver driver = new ChromeDriver();

        /**
         * 浏览器最大化 前进,后退, 刷新
         */
/*        driver.get("http://www.cnblogs.com/itestor");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 浏览器最大化
        driver.manage().window().maximize();

        driver.navigate().to("http://www.baidu.com");
        // 刷新浏览器
        driver.navigate().refresh();
        // 浏览器后退
        driver.navigate().back();
        // 浏览器前进
        driver.navigate().forward();
        // 浏览器退出
        driver.quit();*/


        /**
         * 截图操作
         */
/*        driver.get("http://www.sohu.com");
        File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(srcFile, new File("c:\\test\\2.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        /**
         * 模拟鼠标操作
         */
/*        driver.get("http://www.baidu.com");
        Actions action = new Actions(driver);
        action.contextClick(driver.findElement(By.id("kw"))).perform();*/


        /**
         * 杀掉Windows浏览器进程,
         */
/*       // kill firefox
        WindowsUtils.killByName("firefox.exe");
        // kill IE
        WindowsUtils.killByName("msedge.exe");
        // kill chrome
        WindowsUtils.killByName("chrome.exe");
        // kill WeChat
        WindowsUtils.killByName("WeChat.exe");*/

    }
}

原文地址:https://www.cnblogs.com/itestor/p/15237970.html