【问题标题】:How to take the values from the variable and enter in the text box?如何从变量中获取值并在文本框中输入?
【发布时间】:2021-02-01 11:39:46
【问题描述】:

titles – 是一个包含字符串值的字符串变量

List<String> titles = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='document-card__details']//h3/a"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList());
System.out.print("THE NAME ARE " + titles);

下面是搜索框的 x 路径

driver.findElement(By.xpath("//div[@class='text-filter__input']//input[@placeholder='Search Title']"));

我要执行的操作 - 从标题[变量] 中获取随机值并将其输入到搜索框中。

有什么办法吗?

【问题讨论】:

    标签: java list selenium random sendkeys


    【解决方案1】:

    可以。使用java随机类。

    Random rand = new Random();
    for (int i = 0; i < titles.size() ; i++) {
            String titlesItem = titles.get(rand.nextInt(titles.size()));
            driver.findElement(By.xpath("//div[@class='text-filter__input']//input[@placeholder='Search Title']")).sendKeys(titlesItem);
            
        }
    

    【讨论】:

      【解决方案2】:
          int length = 10;
          boolean useLetters = true;
          boolean useNumbers = false;
          List<String> titles = new ArrayList<>();
          
          titles.forEach(title -> {
              String randomString = RandomStringUtils.random(length, useLetters, useNumbers);
              driver.findElement(By.xpath("//div[@class='text-filter__input']//input[@placeholder='Search Title']")).sendKeys(randomString);
          });
      

      如果需要,您还可以使用一些字母数字字符串。

      谢谢。

      【讨论】:

        【解决方案3】:

        要获取随机标题并将其设置在输入字段中,您可以使用:

        String randomTitle = titles.get(new Random().nextInt(titles.size()));
        
        WebElement searchInput = driver.findElement(By.xpath("//div[@class='text-filter__input']//input[@placeholder='Search Title']"));
        
        searchInput.sendKeys(randomTitle);
        

        【讨论】:

          【解决方案4】:

          要从 List&lt;String&gt; titles 获取随机值,您必须使用 Random() 类,然后使用 sendKeys() 在元素内传递它,您可以使用以下解决方案:

          import java.util.Collections;
          import java.util.Iterator;
          import java.util.List;
          import java.util.Random;
          
          import org.openqa.selenium.By;
          import org.openqa.selenium.support.ui.ExpectedConditions;
          import org.openqa.selenium.support.ui.WebDriverWait;
          
          List<String> titles = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='document-card__details']//h3/a"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList());
          String name = titles.get(new Random().nextInt(titles.size()));
          driver.findElement(By.xpath("//div[@class='text-filter__input']//input[@placeholder='Search Title']")).sendKeys(name);
          

          优化您的代码:

          List<String> titles = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='document-card__details']//h3/a"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList()); 
          driver.findElement(By.xpath("//div[@class='text-filter__input']//input[@placeholder='Search Title']")).sendKeys(titles.get(new Random().nextInt(titles.size())));
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-03-05
            • 2012-02-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-07-11
            相关资源
            最近更新 更多