【问题标题】:How to select a text from the autocomplete textbox using selenium如何使用 selenium 从自动完成文本框中选择文本
【发布时间】:2015-09-01 01:12:11
【问题描述】:

我需要在自动完成文本框中输入一些文本。 然后我将从该自动完成选项中选择一个选项并需要单击它。

我已尝试使用以下代码:

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    String textToSelect = "headlines today";

    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.google.co.in/");
    Thread.sleep(2000);
    WebElement autoOptions= driver.findElement(By.id("lst-ib"));
    autoOptions.sendKeys("he");

    List<WebElement> optionsToSelect = driver.findElements(By.tagName("li"));

    for(WebElement option : optionsToSelect){
        System.out.println(option);
        if(option.getText().equals(textToSelect)) {
            System.out.println("Trying to select: "+textToSelect);
            option.click();
            break;
        }
    }

【问题讨论】:

  • 你的代码有什么问题?
  • 你遇到了什么错误?
  • @VikasNehaOjha 在我的代码中,for 循环迭代没有运行
  • @HelpingHands 它没有显示任何错误!但是,该程序继续运行。在 optionsToSelect 中,它正在获取值列表。我正在尝试在 for 循环 中对其进行迭代并比较值。
  • List&lt;WebElement&gt; optionsToSelect 之前进行睡眠或显式等待。还可以尝试在 for 循环之前打印列表,以便查看列表中的元素数量。

标签: java selenium selenium-webdriver browser-automation


【解决方案1】:
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
String textToSelect = "headlines today";

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
Thread.sleep(2000);
WebElement autoOptions= driver.findElement(By.id("lst-ib"));
autoOptions.sendKeys("he");

List<WebElement> optionsToSelect = driver.findElements(By.xpath("//div[@class='sbqs_c']"));

for(WebElement option : optionsToSelect){
    System.out.println(option);
    if(option.getText().equals(textToSelect)) {
        System.out.println("Trying to select: "+textToSelect);
        option.click();
        break;
    }
}

【讨论】:

    【解决方案2】:

    你可以这样做我以谷歌主页自动建议为例

    public class AutoSelection {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            WebDriver driver = new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("http://www.google.com");
    
            driver.findElement(By.name("q")).sendKeys("mahatama gandhi");
            List<WebElement> autoSuggest = driver.findElements(By
                .xpath("//div[@class='sbqs_c']"));
            // verify the size of the list
            System.out
                .println("Size of the AutoSuggets is = " + autoSuggest.size());
            // print the auto suggest
            for (WebElement a : autoSuggest)
                System.out.println("Values are = " + a.getText());
            // suppose now you want to click on 3rd auto suggest then simply do like
            // this
            autoSuggest.get(2).click();
        }
    }
    

    【讨论】:

    • 感谢您的回复 rajnish。但就我而言,我需要比较该建议中的确切字符串/文本。而不是在 get() 中作为索引值给出。
    • 但是我发现我使用标签名称而不是使用 xpath 的错误。现在它起作用了。非常感谢
    【解决方案3】:
    driverName.findElement(By.xpath("XPATH Location")).sendKeys("KeyNameYouWantToSearch" , Keys.TAB);
    

    【讨论】:

      【解决方案4】:

      我们可以使用 java 8 流 API 来过滤和收集数组中的 Web 元素。使用索引点击会更容易。

      // 收集 5 个自动完成条目列表

      List<WebElement> list = driver.findElements(By.cssSelector("#ui-id-1 
      li:nth-child(n)")).stream()
                  .limit(5)
                  .collect(Collectors.toList());
      

      // 这将点击自动完成列表中的第一个元素

      list.get(0).click();
      

      【讨论】:

        猜你喜欢
        • 2017-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-17
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多