如何基于SQL查询结果行数,用Java+Selenium循环填充对应值?
解决方案:结合SQL查询结果循环执行Selenium操作
步骤1:通过JDBC获取SQL Server中value2列的数据
首先需要引入SQL Server的JDBC驱动(若使用Maven,添加对应mssql-jdbc依赖),编写方法查询TABLE1的value2列,将结果存入列表供后续循环调用。
示例数据查询代码:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class SqlHelper { public static List<String> getValue2List() { List<String> value2List = new ArrayList<>(); // 替换为你的数据库连接信息 String dbUrl = "jdbc:sqlserver://你的数据库地址:1433;databaseName=你的库名;encrypt=true;trustServerCertificate=true"; String dbUser = "数据库用户名"; String dbPwd = "数据库密码"; // try-with-resources自动关闭资源 try (Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT value2 FROM TABLE1")) { while (rs.next()) { value2List.add(rs.getString("value2")); } } catch (Exception e) { e.printStackTrace(); } return value2List; } }
步骤2:重构Selenium代码,封装逻辑并循环执行
把登录、导航到目标页面的逻辑抽离(仅执行一次),然后遍历SQL获取的value2列表,每次循环执行输入、保存操作。同时用显式等待替代Thread.sleep,避免页面加载延迟导致的元素定位失败。
完整整合代码:
package first; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import java.util.List; public class myCodes { public static void main(String[] args) { // 1. 获取SQL返回的value2列表 List<String> value2List = SqlHelper.getValue2List(); if (value2List.isEmpty()) { System.out.println("未获取到数据,终止操作"); return; } // 2. 初始化WebDriver与显式等待 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Steven\\Desktop\\Folder\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); try { // 登录操作(仅执行一次) driver.get("https://testingserver/backend"); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))).sendKeys("admin"); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login"))).sendKeys("randomPassw0rd@"); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='login-form']/fieldset/div[3]/div[2]/button"))).click(); // 导航到目标操作页面(仅执行一次) wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='menu-bbb-backend-stores']/a"))).click(); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='menu-bbb-backend-stores']/div/ul/li[2]/ul/li[2]/div/ul/li[3]/a"))).click(); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='attributeGrid_filter_frontend_label']"))).sendKeys("group" + Keys.ENTER); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='attributeGrid_table']/tbody/tr/td[1]"))).click(); // 3. 循环处理每个value2值 for (String value : value2List) { // 定位输入框,清空后传入当前value wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='manage-options-panel']/table/tbody/tr/td[5]/input"))) .clear() .sendKeys(value); // 点击保存 wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='save']/a"))).click(); // 等待页面回到可编辑状态(根据实际页面调整等待条件) wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='manage-options-panel']/table/tbody/tr/td[5]/input"))); } } catch (Exception e) { e.printStackTrace(); } finally { // 确保浏览器关闭 if (driver != null) { driver.quit(); } } } }
关键注意事项
- JDBC配置:替换代码中的数据库地址、用户名、密码,确保和你的SQL Server环境匹配
- 显式等待:完全替代
Thread.sleep,提升脚本稳定性,避免因页面加载慢导致的异常 - 资源管理:JDBC部分使用try-with-resources自动关闭连接等资源,避免内存泄漏
- 循环状态恢复:每次保存后等待页面回到可编辑状态,确保下一次循环的元素可正常定位
内容的提问来源于stack exchange,提问作者JustToKnow
相关产品推荐
相关产品推荐

