栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

selenium模拟网页操作

Java 更新时间:发布时间: 百科书网 趣学号

1、新建一个工程 选择springboot版本为2.6.8

2、添加依赖 pom.xml


        
            org.springframework.boot
            spring-boot-starter
        

        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-aop
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.10
        
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.3
        
        
        
            mysql
            mysql-connector-java
            8.0.15
        

        
        
            org.seleniumhq.selenium
            selenium-api
            3.141.59
        
        
            org.seleniumhq.selenium
            selenium-chrome-driver
            3.141.59
        
        
            org.seleniumhq.selenium
            selenium-support
            3.141.59
        

        
        
            org.junit.platform
            junit-platform-suite-api
            1.8.2
        
        
            org.junit.jupiter
            junit-jupiter-api
            5.8.2
        
        
            org.junit.platform
            junit-platform-suite
            test
            1.8.2
        
        
            org.springframework
            spring-test
        
        
            org.springframework.boot
            spring-boot-test
        

    

3、配置信息

server:
  port: 8099

spring:
  main:
    # 解决升级 Spring Boot 2.6后,因循环引用导致启动时报错的问题
    allow-circular-references: true
  datasource:
    url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: root

mybatis:
  mapper-locations:
    - classpath:mappermapper/*.xml

4、根据自己的浏览器下载对应的驱动 下载地址 http://chromedriver.storage.googleapis.com/index.html


复制驱动 chromedriver.exe 到项目中 src/main/resources

5、test层编写代码

@Test
public void baiduLogin() throws InterruptedException {

    // 加载浏览器驱动
    String os = System.getProperty("os.name");
    System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");

    // 访问地址
    String url = "https://www.baidu.com/";

    // 后台运行谷歌浏览器设置
    ChromeOptions chromeOptions = new ChromeOptions();
    // 隐藏‘Chrome正在受到自动软件的控制’ chromeOptions.addArguments("disable-infobars");已废弃
    chromeOptions.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
    chromeOptions.addArguments("window-size=1920,1080");

    // 打开chrome浏览器
    ChromeDriver webDriver = new ChromeDriver(chromeOptions);
    webDriver.manage().window().maximize();
    webDriver.manage().deleteAllCookies();
    // 与浏览器同步非常重要,必须等待浏览器加载完毕
    webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);

    // 访问地址 此时能跳转到百度首页
    webDriver.get(url);

    // 点击登录按钮
    webDriver.findElement(By.xpath("/html/body/div[1]/div[1]/div[4]/a")).click();

    // 输入用户名和密码
    Thread.sleep(1000);
    webDriver.findElement(By.xpath("/html/body/div[5]/div[2]/div[2]/div/div/div/div/div/div[1]/div[2]/form/p[3]/input[2]")).sendKeys("用户名");

    webDriver.findElement(By.xpath("/html/body/div[5]/div[2]/div[2]/div/div/div/div/div/div[1]/div[2]/form/p[4]/input[2]")).sendKeys("密码");

    // 点击登录
    Thread.sleep(1000);
    webDriver.findElement(By.xpath("/html/body/div[5]/div[2]/div[2]/div/div/div/div/div/div[1]/div[2]/form/p[8]/input")).click();

}
转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/956545.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号