使用java的Selenium WebDriver 自动化修改某值并保存

比如我用自己本地搭建的某个网站

修改环境变量中的第二行的备注内容:

 

  登录页面:

 

 

 

 

运行后的修改结果为:

 

 登录后台查看

 

代码:

 

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;


public static void main(String[] args) throws InterruptedException {
        // 设置Chrome浏览器驱动程序的路径
        System.setProperty("webdriver.chrome.driver", "/your path/chromedriver_mac64/chromedriver");

        // 创建一个Chrome浏览器对象
        WebDriver driver = new ChromeDriver();

        // 判断是否登录
        driver.get("http://10.11.90.249:5700/env");

        // 检查是否已登录
        boolean loggedIn;
        try {
            WebElement userMenu = driver.findElement(By.className("ant-layout-sider-children"));
            loggedIn = userMenu.isDisplayed();
        } catch (Exception e) {
            // 如果找不到用户菜单,则说明未登录
            loggedIn = false;
        }
        WebDriverWait wait = new WebDriverWait(driver, 30);
        
        if(!loggedIn) {
            driver.get("http://10.11.90.249:5700/login");
            // 获取搜索框元素
            WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
            WebElement password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password")));
            // 清除输入框中的文本并输入新的文本
            username.clear();
            username.sendKeys("123");

            password.clear();
            password.sendKeys("123");

            // 提交表单
            WebElement submitButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("ant-btn-primary")));
            submitButton.click();

            // 等待页面加载完成
            Thread.sleep(3000);
        }
        // http://10.11.90.249:5700/env

        // 再打开
        driver.get("http://10.11.90.249:5700/env");
        // 点击表格中第二行的编辑按钮
//        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//tr[@data-row-key='2']//div[@class='ant-space-item']//a[@class='anticon-edit']"))).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"root\"]/div/section/div[2]/main/div/div[2]/div/div/div/div/div/div/div/div[2]/table/tbody/tr[3]/td[8]/div/div[1]/a"))).click();
        
        
        WebElement env_modal_remarks = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("env_modal_remarks")));
        // 清除输入框中的文本并输入新的文本
        env_modal_remarks.sendKeys("asi test 自动化输入");


        WebElement re_submitButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(concat(' ', normalize-space(@class), ' '), ' ant-modal-footer ')]//button[contains(@class, 'ant-btn-primary')]")
        ));
        re_submitButton.click();
        
        Thread.sleep(3000);
        
        // 关闭浏览器
        driver.quit();
    }

 

 

我下载的ChromeDriver版本是:111.0.5563.64

 

依赖为:

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

 

posted @ 2023-03-31 14:08  ToDarcy  阅读(188)  评论(0编辑  收藏  举报