Java selenium 基本使用

此功能是抓取本地文件里面的数据,然后填充到web应用上的指定输入框

1.首先下载 msedgedriver

2 其次获取Xpath

3.配置selenium 仓库地址

java 复制代码
package com.example.automation;

import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;

import java.io.*;

public class WorkOrderAutomation {
    public static void main(String[] args) throws InterruptedException, IOException {

        System.setProperty("webdriver.edge.driver", "C:\\Windows\\System32\\msedgedriver.exe");

        WebDriver driver = new EdgeDriver();

        driver.get("http://localhost:3000/workorder/workorder");
        Thread.sleep(20000);
        String myXPath ="//input[@placeholder='请输入工单名称']";
        BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\zhihu.wang\\Desktop\\LotSN.txt"));
        String data;
        while((data = reader.readLine()) != null){

            try {
                //查找元素
                WebElement element = driver.findElement(By.xpath(myXPath));
                element.clear();
                //输入值
                element.sendKeys(reader.readLine());
                //回车
                element.sendKeys(Keys.RETURN);
                //延迟2秒
                Thread.sleep(2000);

            } catch (Exception e) {
                System.out.println("错误:" + e.getMessage());
            }
        }
        //等待五秒关闭浏览器
        Thread.sleep(5000);
        driver.quit();
    }
}

4.有时候复制的Xpath不准,需要在浏览器控制台运行下面代码获取Xpath。

// 查找所有非隐藏输入框

var inputs = Array.from(document.querySelectorAll('input:not([type="hidden"])'));

inputs.forEach((input, index) => {

console.log(`[${index+1}]`, {

placeholder: input.placeholder,

id: input.id,

name: input.name,

class: input.className,

type: input.type

});

});

相关推荐
寻寻觅觅☆7 小时前
东华OJ-基础题-106-大整数相加(C++)
开发语言·c++·算法
l1t8 小时前
在wsl的python 3.14.3容器中使用databend包
开发语言·数据库·python·databend
0思必得08 小时前
[Web自动化] Selenium无头模式
前端·爬虫·selenium·自动化·web自动化
青云计划8 小时前
知光项目知文发布模块
java·后端·spring·mybatis
赶路人儿8 小时前
Jsoniter(java版本)使用介绍
java·开发语言
ceclar1239 小时前
C++使用format
开发语言·c++·算法
探路者继续奋斗9 小时前
IDD意图驱动开发之意图规格说明书
java·规格说明书·开发规范·意图驱动开发·idd
码说AI9 小时前
python快速绘制走势图对比曲线
开发语言·python
Gofarlic_OMS9 小时前
科学计算领域MATLAB许可证管理工具对比推荐
运维·开发语言·算法·matlab·自动化
星空下的月光影子10 小时前
易语言开发从入门到精通:补充篇·网络爬虫与自动化采集分析系统深度实战·HTTP/HTTPS请求·HTML/JSON解析·反爬策略·电商价格监控·新闻资讯采集
开发语言