问题描述
我已成功从 Excel 文件中读取数据,该文件包含三列(URL/FQDN、用户名、密码)。 此 Excel 表由我的各种服务器的 FQDN 组成。我还有另一个功能需要在所有这些服务器上执行某些任务。 由于我对 Selenium/java 非常陌生,因此对于如何在不同浏览器窗口上打开所有 url 并为每个窗口传递用户名和密码这一事实,我将不胜感激。稍后我可以尝试管理我需要对所有这些执行的任务/相同的操作 这是我迄今为止编写的用于读取 Excel 表格的代码段-:
公共类修改{
public static FileInputStream fis;
public static XSSFWorkbook wb;
public static XSSFSheet sheet;
public static String row;
public static XSSFCell cell;
public static void main(String[] args) {
try {
getCellData();
} catch (IOException e) {
// Todo Auto-generated catch block
e.printstacktrace();
}
}
public static void getCellData() throws IOException
{
fis=new FileInputStream("C:\\Users\\windows\\Desktop\\hostname.xlsx");
wb=new XSSFWorkbook(fis);
sheet=wb.getSheet("hostname");
int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
//Create a loop over all the rows of excel file to read it
for (int i = 0; i < rowCount+1; i++) {
Row row = sheet.getRow(i);
//Create a loop to print cell values in a row
for (int j = 0; j < row.getLastCellNum(); j++) {
//Print Excel data in console
System.out.print(row.getCell(j).getStringCellValue() + " ");
}
System.out.println();
}
}
}
解决方法
首先看看你的函数的访问修饰符(总是非常重要的!)。仅在单个类中使用的函数应该始终是私有的。
你想做的事情其实并不难实现。只需编写一个(私有)函数,并提供 3 个参数:url、用户名和密码。您将在 getCellData() 函数的 for 循环中调用此函数。关于如何准确启动新窗口(或我认为在 imo 上会更好的选项卡),应该非常简单。我得谷歌一下!只需导入 Selenium WebDriver 库,然后读入即可!这里有很多功能可以帮助您实现目标。
,假设 getCellData()
至少返回此实例的 URL 列表。
一旦它被执行,你就会有这样的东西:
代码:
public String[] getCellData() {
return new String[]{"https://www.google.co.in","https://www.youtube.com","https://stackoverflow.com"};
}
在新标签页中打开每个网址:
@Test
public void testSO() throws InterruptedException {
try {
String[] allUrls = getCellData();
System.out.println(allUrls.length);
int count = 1;
for(String url : allUrls) {
driver.get(url);
String windowsBefore = driver.getWindowHandle();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.open('');");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(count));
count = count + 1;
Thread.sleep(3);
}
} catch (Exception e) {
e.printStackTrace();
}
}