首页 > 编程语言 >java操作selenium浏览器自动化操作

java操作selenium浏览器自动化操作

时间:2022-08-25 17:37:58浏览次数:78  
标签:java selenium driver org import 浏览器 操作

selenium github

selenium官网

各类型浏览器webDriver驱动下载

chrome浏览器webDriver驱动下载,注意要与电脑上实际安装的浏览器版本相对应

原理说明:

java代码直接通过selenium-java库中的核心类ChromeDriver调用本地下载的webDriver驱动,webDriver驱动会自动找到其对应操作系统安装的浏览器模仿人类执行相关的操作

POM依赖

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>

selenium-java依赖版本过高会导致下方异常

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/internal/Require
....
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.internal.Require

参考:https://blog.csdn.net/m0_56758840/article/details/123380448

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.awt.image.BufferedImage;

/**
 * @author JHL
 * @version 1.0
 * @date 2022/8/25 14:50
 * @since : JDK 11
 */
public class T {

    public static void main(String[] args) throws InterruptedException {
        // 设置jvm系统属性,selenium会读取这个数据找到要使用的webDriver驱动
        System.setProperty("webdriver.chrome.driver",
                           "D:/env/chromedriver_win32/chromedriver.exe");
		// 设置驱动的选项
        ChromeOptions options = new ChromeOptions();
        // 设置无头浏览器,即不打开窗口渲染,都在内存中执行
        options.setHeadless(true);
        // 根据选项实例化驱动
        ChromeDriver driver = new ChromeDriver(options);
        // 访问百度,并搜索hello world
        driver.get("http://www.baidu.com");
        driver.findElement(By.name("wd")).sendKeys("hello word!");
        driver.findElement(By.id("su")).click();
        Thread.sleep(5000);
        // 搜索结果页面的html源码
        System.out.println(driver.getPageSource());
        
        // 截图方式一
        // File file = driver.getScreenshotAs(OutputType.FILE);

        // 截图方式二
        BufferedImage screenshot = driver.getScreenshotAs(new OutputType<>() {
            @Override
            public BufferedImage convertFromBase64Png(String base64) {
                return ImgUtil.toImage(base64);
            }
            @Override
            public BufferedImage convertFromPngBytes(byte[] bytes) {
                return ImgUtil.toImage(bytes);
            }
        });
        ImgUtil.write(screenshot, FileUtil.newFile("截图.png"));
        driver.quit();
    }

使用参考:

https://blog.csdn.net/chenjxj123/article/details/121802904

https://www.cnblogs.com/ychun/p/14282422.html

标签:java,selenium,driver,org,import,浏览器,操作
From: https://www.cnblogs.com/hhddd-1024/p/16624972.html

相关文章

  • Caused by: java.lang.UnsupportedClassVersionError: com/hfplm/handler/HFEBOMation
    Causedby:java.lang.UnsupportedClassVersionError:com/hfplm/handler/HFEBOMationHandlerhasbeencompiledbyamorerecentversionoftheJavaRuntime(classf......
  • Java 连接 MySQL
    让Java和MySQL连接起来-囧雪诺-博客园 https://www.cnblogs.com/jonsnow/p/6246131.htmlJava连接MySQL需要驱动包,可以下载菜鸟教程提供的 jar包:http://stati......
  • 【Java】LambdaStream
    JavaLambdaStreamFactoryimportjava.util.*;importjava.util.stream.*;publicclassLambdaStream{publicstatic<T>Stream<T>of(Spliterator<T>split......
  • python里的简洁操作
    1、lambda匿名函数好处精简代码,lambda省去了定义函数,map省去了写for循环过程:res=list(map(lambdax:'test'ifx==''elsex,a))    ......
  • Java生成带logo的二维码,并将二维码添加到图片中
    1.pom.xml<!--生成二维码--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-extra</artifactId><version>5.4.3</version></dependency><d......
  • java的String.format中的百分号
    System.out.println(String.format("百分比%.2f%",(float)80/90));错误信息:Exceptioninthread"main"java.util.UnknownFormatConversionException:Conversion=......
  • JavaScript中改变鼠标指针样式的方法
    JavaScript中改变鼠标指针样式的方法    在js中我们可以通过style对象的cursor属性来设置鼠标指针的样式,例varbody=document.querySelector("body") body.style......
  • JAVA---06
    第六天1.instanceof和类型转化instanceof:用于比较两个对象是否有继承关系类型转化:低可以直接转高Person //父类Student//子类   //子类转化为父类......
  • 第一个JAVA小程序
    HelloWorld随便新建一个文件,修改后缀名称为“.java”需要注意的地方:注意大小写,注意各个类的英文单词拼写不能写错在编写程序时尽可能使用英文书写(哪怕输出......
  • 学习:python操作数据库(二)
    python连接数据库下载第三方包    创建表   ......