How to select value from the auto suggestion text box?

后端 未结 4 1125
刺人心
刺人心 2021-01-29 11:12

From text boxI have tried many methods to find out the solution but I failed, So please help me regarding this Query

Website:- https://www.goibibo.com/

Inside th

4条回答
  •  孤独总比滥情好
    2021-01-29 11:22

    As it is auto suggesting the content and you want to select the first option from that drop down, you can use selenium's Keys enum and you can perform the selection like below :

    driver.get("https://www.goibibo.com/");
    WebElement from = driver.findElement(By.id("gosuggest_inputSrc"));
    from.sendKeys("Bangalore");
    
    Thread.sleep(3000);
    from.sendKeys(Keys.ARROW_DOWN +""+ Keys.ENTER);
    

    If you want to select other option than the first one then you can use the below xpaths to identify that drop down options :

    //input[@id='gosuggest_inputSrc']/preceding-sibling::i/following::ul[contains(@id, 'react-autosuggest')]//li
    

    Or

    //ul[contains(@id, 'react-autosuggest')]//li
    

    Below is the code to print all the options from that drop down and to select the particular value :

    driver.get("https://www.goibibo.com/");
    WebElement from = driver.findElement(By.id("gosuggest_inputSrc"));
    from.sendKeys("Bangalore");
    
    // Giving some delay so that the auto suggestion drop down will appear      
    Thread.sleep(3000);
    // Fetching options from dropdown
    List dropdownOptions = driver.findElements(By.xpath("//ul[contains(@id, 'react-autosuggest')]//li"));
    // Printing all the option text
    for(WebElement element : dropdownOptions) {
        System.out.println(element.getText());
    }
    // Selecting the first option
    dropdownOptions.get(0).click();
    

    I hope it helps...

提交回复
热议问题