【问题标题】:InvalidArgument Exception is thrown in Selenium eventhough script runs fine as expected在 Selenium 中抛出 InvalidArgument 异常,即使脚本按预期运行良好
【发布时间】:2019-07-03 14:22:48
【问题描述】:

我的第一个问题在这里!如果我遗漏了什么,请原谅!

我已经编写了代码来从 excel 中选择 URL,然后通过从 excel 表中获取相应的数据来单击 url 中的元素。该脚本按预期运行良好。但是在最后一次循环运行后它仍然抛出 Invalid Argument 异常。没有错误(至少在我看来),我已经验证了 excel 中的 url 链接。他们很好。脚本当前以我想要的方式运行。但我仍然得到了例外。不执行 driver.close 的最后一步。请检查代码,让我知道我哪里出错了。

excel 的第一列是 url。以下各列包含由脚本拾取并用于链接文本命令以单击 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();
    
}
}   

目前在脚本运行执行后得到这个:

Exception in thread "main" org.openqa.selenium.InvalidArgumentException: invalid argument

【问题讨论】:

  • 能否请您发布完整的堆栈跟踪?
  • @Homewrecker 这是完整的代码。我错过了什么吗?请告诉我
  • @Tyrion 完整的堆栈跟踪将准确说明哪个步骤引发了错误。可以发一下吗?
  • getLastRowNum 是从零开始的...(您要加 1)您可能还想检查空值/空值
  • 感谢@pcalkins 和其他愿意提供帮助的人。我玩弄了代码,这确实是 excel 表中的空值/空值的问题。我清理了数据,现在它工作正常!

标签: selenium exception invalidargumentexception


【解决方案1】:

您可以检查 null,如下所示。这样,即使您在 excel 中有空数据,它也可以工作。

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");


}
}
}

【讨论】:

  • 谢谢。这解决了它。该脚本在第二个 if 条件内循环。但我已经纠正了它,它现在工作正常.. 谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 2013-04-09
  • 2017-05-22
  • 1970-01-01
相关资源
最近更新 更多