0

在我的脚本中,根据上一步中的选择,会出现一个动态下拉菜单。我的代码在菜单存在时有效,但当它不是我的脚本时失败。我正在使用 strBusinessType 变量从我的 SQL 数据库中引入数据。

//Select Business Type (If Present)
owd.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
boolean exists = owd.findElements( By.id("BusinessType1") ).size() !=0;
owd.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

if (exists){
    new Select(owd.findElement(By.id("BusinessType1"))).selectByVisibleText(strBusinessType);
}
else{
    System.out.println("Business Type not present");
}
4

1 回答 1

0

尝试在 try 块中包围您的代码,并在元素不存在时捕获异常。

try {
owd.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
boolean exists = owd.findElements( By.id("BusinessType1") ).size() !=0;
owd.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

if (exists){
    new Select(owd.findElement(By.id("BusinessType1"))).selectByVisibleText(strBusinessType);
  }
}
catch (Exception e){
    System.out.println("Business Type not present");
}
于 2013-10-30T07:32:06.797 回答