首页 > 解决方案 > 如何从变量中获取字符串值并发送到搜索框

问题描述

'name_asset' 是一个包含字符串值的字符串变量。

我想根据索引将这些值随机发送到搜索框。有什么办法吗?

List<WebElement> Asset = driver.findElements(By.xpath("//div[@class='document-card__details']//h3"));

        for (WebElement list_value : Asset) {
            name_asset = list_value.getText();

            if (name_asset != null) {

                System.out.println("the vales are *****" + name_asset);
            } else {

                System.out.println("Assets count is zero");
            }

        }

    }

标签: javaselenium

解决方案


List<WebElement> Asset = driver.findElements(By.xpath("//div[@class='document-card__details']//h3"));
List<String> assetList= new ArrayList<String>();

        for (WebElement list_value : Asset) {
            name_asset = list_value.getText();

            if (name_asset != null) {
                 
                assetList.add(name_asset)
                System.out.println("the vales are *****" + name_asset);
            } else {

                System.out.println("Assets count is zero");
            }

        }

    }


   //to access text randomly
  Random rand = new Random();
  int index= rand.nextInt(assetList.size());
   assetList.get(index)

将文本存储到列表并使用随机索引


推荐阅读