Ich versuche, Daten aus einer HTML-Tabelle in einem 2D-Array zu speichern, aber ich kann es nicht herausfinden. Mein Code sieht momentan so aus.Speichern von Daten aus einer HTML-Tabelle in ein 2D-Array Selenium Java
Dies ist der Ort, wo der Tisch ist: https://www.w3schools.com/html/html_tables.asp
@Test
public void checkTable() {
// Number of Rows in table
int rowCount = driver.findElements(By.xpath("//*[@id='customers']/tbody/tr")).size();
System.out.println("The total number of rows is " + rowCount);
// Number of Columns in table
int colCount = driver.findElements(By.xpath("//*[@id='customers']/tbody/tr[1]/th")).size();
System.out.println("The total number of columns is " + colCount);
// Dynamic X-Path
String firstX = "//*[@id='customers']/tbody/tr[";
String secondX = "]/td[";
String thirdX = "]";
for(int i=2; i <= rowCount; i++) {
for(int j=1; j <= colCount; j++) {
String completeX = firstX + i + secondX + j + thirdX;
String tableData = driver.findElement(By.xpath(completeX)).getText();
System.out.println(tableData + " ");
}
System.out.println(" ");
}
Und die aktuelle Ausgabe.
Alfreds Futterkiste
Maria Anders
DeutschlandCentro Comercial Moctezuma
Francisco Chang
MexikoErnst Handel
Roland Mendel
ÖsterreichInsel Handels
Helen Bennett
UKLachen Bacchus Winecellars
Yoshi Tannamuri
KanadaMagazzini Alimentari Riuniti
Giovanni Rovelli
Italien
Ich versuche, Holen Sie diesen Ausgang zu lo ok so etwas.
[Alfreds Futterkiste, Maria Anders, Deutschland], [Centro comercial Moctezuma, Francisco Chang, Mexiko]] ... etc.
Hallo! Es scheint zu funktionieren, aber ich wollte die Informationen tatsächlich in einem 2D-Array speichern, anstatt nur das Logging zu protokollieren, was wie ein Array aussieht. Ich bin noch ziemlich neu in Java. Bitte helfen Sie. – Niko
@niko - Brauchen Sie Klammern '[' und Kommas ',' auch? – Kapil