Selenium 混合框架中的 selectByIndex(data) 用于下拉列表的随机值

selectByIndex(data) in Selenium Hybrid Framework for random values of dropdown

我一直在使用混合框架,即对象、数据和关键字。我一直坚持根据其索引选择下拉菜单的随机值。

在我提到测试数据的输入文件中 Test Data 有一个整数列 WorkGroup 即我以整数形式为 Dropdown 提供了输入,它只是下拉列表的索引值。现在在第二张图片中,您可以看到 se.selectByIndex(data) 造成了麻烦。

错误是:

The method selectByIndex(int) in the type Select is not applicable for the arguments (String)

Code where error comes

public  String selectDropdown(String object,String data){
    APP_LOGS.debug("Selecting dropdown values.");               
    try{
        WebElement _directoryDDL = driver.findElement(By.id(OR.getProperty(object)));
            Select se = new Select(_directoryDDL);
            se.selectByIndex(data); // This is where error is coming. I need to use only index.
            return Constants.KEYWORD_PASS;
        }
    catch(Exception e1){
        return Constants.KEYWORD_FAIL+" Control not found.";
        }
    }

我不能使用 selectByValueselectByVisibleText 因为每个用户的下拉值都会改变。这就是为什么只有索引值是这里唯一的解决方案。

selectByIndex 方法需要一个 Integer 参数,而您传递的参数 data 是一个 String 类型。这就是它给出错误的原因。这是将字符串转换为整数的代码。

se.selectByIndex(Integer.parseInt(data));

如错误所述,您正在将字符串传递到需要 int 的位置。使用 Integer.parseInt(String s)Integer.parseUnsignedInt(String s)

只需根据您在 Selenium WebDriver 中使用的语言将字符串转换为整数。

Convert a String to a Number

确保输入的字符串是数字,即“1”、“0”等。否则会出现异常。