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

});

});

相关推荐
CAE虚拟与现实17 小时前
docker desktop中的build功能是要build什么
java·docker·容器
PHP实战开发录18 小时前
PHP脚本手动能跑定时任务却失败
开发语言·php·开发
郝学胜-神的一滴19 小时前
Effective Python 条款 10 :海象运算符_=
开发语言·python·程序人生·开源
wuyk55519 小时前
Python零基础入门第十四章:异常处理(try-except)
开发语言·python
wuyk55519 小时前
【Socket 进阶之路】第 3 章 TCP 三次握手 & 四次挥手深度剖析|连接建立、断开、状态机、TIME_WAIT 核心工程问题
服务器·开发语言·网络·物联网·网络协议·tcp/ip
allnlei19 小时前
s6-overlay - 装在 Docker 容器里的轻量级管家
java·docker·容器
彧azz1 天前
图的存储结构详解:邻接矩阵的原理、实现与应用
开发语言·数据结构·学习·php
IT_Octopus1 天前
IntelliJ 本地日志路径自定义:`-DLOG_PATH=./logs` 为什么总“不听话“
java·log4j·intellij-idea
嵌入式学习菌1 天前
Modbus‑RTU 数据类型分析
开发语言·单片机·bug
长征coder1 天前
【无标题】
java·性能优化