Here is the full code, the error is coming in the line ["xlutil.setCellData(...)";], I have highlighted it with the comment "ERROR HERE". The rest of the code is simply to navigate to the page where there is a webtable. The purpose is to copy a value from the webtable into an excel file...
But the thing is, I copied the entire file from another laptop, where it worked seamlessly. The function is completely same in both systems, and yet there is this error only in this laptop.
The error exactly reads: Exception in thread "main" java.lang.NoSuchMethorError: 'org.apache.logging.log4j.LogBuilder org.apache.log4j.Logger.atDebug()'
package packageOne;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.time.Duration;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.locators.RelativeLocator;
public class shilpi {
public static void main(String[] args) throws Throwable {
int rows;
String lgrpath;
float[] lgr = { 0, 0, 0, 0, 0, 0, 0, 0 };
WebDriver driver = new FirefoxDriver();
WebDriverManager.firefoxdriver().setup();
String[] acc = { "I6", "I7", "I8", "I9", "I501", "I201", "I88", "I4" };
String url = "https://bo.competentfinman.com:1467/capexweb/capexweb/";
driver.get(url);
driver.switchTo().frame(driver.findElement(By.name("main")));
driver.findElement(By.name("dfuserid")).sendKeys("**");
driver.findElement(By.name("dfpassword")).sendKeys("********");
driver.findElement(By.name("B1")).click();
driver.switchTo().defaultContent();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
try {
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("middle"));
} catch (Exception e) {
}
try {
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[1]/a")));
} catch (Exception e) {
}
driver.findElement(By.xpath("/html/body/div[1]/a")).click(); // Continue >>
driver.switchTo().defaultContent();
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
try {
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("middle"));
} catch (Exception e) {
}
driver.findElement(
By.cssSelector("#Table2>tbody:nth-child(1)>tr:nth-child(1)>td:nth-child(1)>input:nth-child(3)"))
.click(); // NSE-FO 2023-24
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
driver.switchTo().defaultContent();
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
try {
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("contents"));
} catch (Exception e) { System.out.println("Caught exception");
}
try {
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#itemTextLink3")));
} catch (Exception e) { System.out.println("Caught exception");
}
driver.findElement(By.cssSelector("#itemTextLink3")).click(); // Financial
driver.findElement(By.id("itemTextLink6")).click(); // Fin. Lgr.
for (int i = 0; i < 8; i++) {
driver.switchTo().defaultContent();
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
try {
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("middle"));
} catch (Exception e) {
}
try {
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"dfclientid\"]")));
} catch (Exception e) { System.out.println("Caught exception");
}
Select dropdown = new Select(driver.findElement(By.xpath("//*[@id=\"dfclientid\"]")));
dropdown.selectByValue(acc[i]);
driver.findElement(By.name("B1")).click(); // GO
driver.switchTo().defaultContent();
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
try {
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("middle"));
} catch (Exception e) {
}
try {
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/table[2]/tbody/tr")));
} catch (Exception e) {
}
rows = driver.findElements(By.xpath("/html/body/table[2]/tbody/tr")).size();
rows--;
lgrpath = ("/html/body/table[2]/tbody/tr[" + rows + "]/td[2]/b");
lgr[i] = Float.parseFloat(driver.findElement(By.xpath(lgrpath)).getText());
XLUtility xlutil = new XLUtility("\\C:\\Users\\Acer A515\\Desktop\\Book1.xlsm");
// **ERROR HERE**
xlutil.setCellData("sheetone", i, 0, acc[i]);
xlutil.setCellData("sheetone", i, 1, lgr[i]);
System.out.println("Shoved " + lgr[i] + " up yours");
// Go back to go to next account:
driver.navigate().back();
}
}
}
I tried changing the file path before it, I tried changing the name of the function in both this class and the definition class. But nothing worked...
Here is the list of errors at the bottom in Eclipse:
Exception in thread "main" java.lang.NoSuchMethodError: 'org.apache.logging.log4j.LogBuilder org.apache.logging.log4j.Logger.atDebug()'
at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.parseRelationshipsPart(PackageRelationshipCollection.java:309)
at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.<init>(PackageRelationshipCollection.java:160)
at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.<init>(PackageRelationshipCollection.java:130)
at org.apache.poi.openxml4j.opc.PackagePart.loadRelationships(PackagePart.java:565)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:745)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:315)
at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:59)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:289)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:285)
at packageOne.XLUtility.setCellData(XLUtility.java:154)
at packageOne.shilpi.main(shilpi.java:115)
I tried to redo the whole thing with a Java project, the same error came. Here is the stacktrace:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream
at JavaPackage.XLUtility.setCellData(XLUtility.java:153)
at JavaPackage.shilpi.main(shilpi.java:110)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 2 more
I fixed it by including another dependency in the pom.xml, by the name of SLF4J Reload4j Provider 2.0.9