Warm tip: This article is reproduced from stackoverflow.com, please click
selenium exception invalidargumentexception

InvalidArgument Exception is thrown in Selenium eventhough script runs fine as expected

发布于 2020-03-27 10:22:32

My first question here! Pardon if i am missing out anything!

I have written code to pick URL from excel and then click on elements in url by fetching corresponding data from the excel sheet. The script runs fine as expected. But it is still throwing Invalid Argument exception after the final run of loop. There are no errors (atleast to my eyes) and i have verified the url links in excel. They are fine. Script currently behaves the way i want. But still i am getting the exception. The final step of driver.close is not performed. Please check the code and let me know where i went wrong.

First column in excel is url. Following columns has text which is picked up by script and used for linktext command to click on the elements in url.

package newpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InvalidObjectException;
import java.io.IOException;
 
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelRead {
public static void main(String[] args) throws Exception {
           	System.setProperty("webdriver.chrome.driver","C:\\driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebDriverWait wait=new WebDriverWait(driver, 20);
String baseUrl = "https://google.com/";
driver.get(baseUrl);
        
            
FileInputStream fis = new FileInputStream("D:\\test_data.xlsx");
@SuppressWarnings("resource")
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);

int s=sheet.getLastRowNum()+1;
     
for(int i=0; i<s; i++)
{
int k = sheet.getRow(i).getLastCellNum();
XSSFCell cell=sheet.getRow(i).getCell(0);	
String url=cell.toString();
driver.get(url); 
Thread.sleep(3000);
System.out.println("Launched "+url);
Thread.sleep(5000);
        
for(int j=1;j<k;j++)
{
               
                
String data0=sheet.getRow(i).getCell(j).getStringCellValue();
driver.findElement(By.linkText(data0)).click();  
  
System.out.println(data0+" Saved");
                    
      
}
}
     
            
driver.close();
    
}
}   

Currently getting this after script runs execution:

Exception in thread "main" org.openqa.selenium.InvalidArgumentException: invalid argument
Questioner
Tyrion
Viewed
83
Murthi 2019-07-04 17:25

You can check for null as given below. so that, even though you have empty data in excel, it will work.

for(int i=0; i<s; i++)
{
int k = sheet.getRow(i).getLastCellNum();
XSSFCell cell=sheet.getRow(i).getCell(0);   
String url=cell.toString();
if(url!=null && !url.isEmpty())
{
   driver.get(url); 
   Thread.sleep(3000);
   System.out.println("Launched "+url);
   Thread.sleep(5000);

for(int j=1;j<k;j++)
{
String data0=sheet.getRow(i).getCell(j).getStringCellValue();
if(data0!=null && !data0.isEmpty())
   driver.findElement(By.linkText(data0)).click();  

System.out.println(data0+" Saved");


}
}
}