1
Driver.findElement(By.xpath("//*[@id='client']")).sendKeys("Ho");
Driver.manage().timeouts().implicitlyWait(1,TimeUnit.MINUTES);

WebElement dropdown = (new WebDriverWait(Driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='client']")));

Driver.findElement(By.xpath("//*[@id='collapseClientInfo']/div/form/div[3]/div[2]/ul/li[1]/a")).sendKeys(Keys.ENTER);

Could you please help me to select auto populate value from drop down list:

  1. We've Client textbox which is an auto-populate box.
  2. When I enter "ho" text in the client field, it shows me the drop down which has values related to my entered text i.e. ho, then I have to select those values which are available under list.
  3. In above code I've tried with Press Enter but unable to select the value.

Could you please check the above code and help me out for the same?

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
ajay kumar
  • 73
  • 1
  • 9
  • Could you share the website URL or relevant HTML??.... – Saurabh Gaur Feb 14 '17 at 12:02
  • package ChromeBrowser; public static void main(String[] args) { Driver.findElement(By.xpath("//*[@id='client']")).sendKeys("Ho"); Driver.manage().timeouts().implicitlyWait(1,TimeUnit.MINUTES); WebElement dropdown = (new WebDriverWait(Driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='client']"))); Driver.findElement(By.xpath("//*[@id='collapseClientInfo']/div/form/div[3]/div[2]/ul/li[1]/a")).sendKeys(Keys.ENTER); }} Please check above code. – ajay kumar Feb 14 '17 at 12:08
  • But where we can check your code.?? – Saurabh Gaur Feb 14 '17 at 12:09
  • Sourabh, Sorry this is my company website i cant share the credential, I can share the specific information for the same? – ajay kumar Feb 14 '17 at 12:12
  • ..........................................this is the html for that field – ajay kumar Feb 14 '17 at 12:13
  • @SaurabhGaur Is this fine or you need some more information for the same? – ajay kumar Feb 14 '17 at 12:15
  • But you are able to set value on this field, need to share problematic HTML....You can share screenshot as well..:) – Saurabh Gaur Feb 14 '17 at 12:15
  • 1
    URL - https://bioceptbetaweb.azurewebsites.net/ Username - ajay.kumar@technossus.com PWD- Ajay@123 After login, please click on 'Place a new order' Then on Client field we've to select value from autopopulate popup. Please check – ajay kumar Feb 14 '17 at 12:18
  • @SaurabhGaur have you got the problem ? – ajay kumar Feb 14 '17 at 12:30
  • Yes, I have provided the solution try it and let me know..:) – Saurabh Gaur Feb 14 '17 at 12:53

3 Answers3

2

You should try as below :-

WebDriverWait wait = new WebDriverWait(Driver, 60);

//wait until loader invisible
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loaderDiv")));

//this sleep is required because after invisibility of loader focus goes to first input which is Requisition Number
//If you are filling form from first input no need to for this sleep
//if you want to input directly to client field need to sleep to avoid focus first  
Thread.sleep(3000);

//Now find the client input and set value
WebElement client = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("client")));
client.sendKeys("Ho");

//Now find all the showing option   
List<WebElement> dropdownOptions = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("ul.dropdown-menu a")));

//Now select the first option
dropdownOptions.get(0).click();
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • 1
    Thank you for the code. It works fine but now I've tried the same thing in next field and unable to find the element. In the above code, you've selected "CSS Selector" that you have selected from the code, right? – ajay kumar Feb 15 '17 at 07:58
  • @ajaykumar Yes, May be there are some different selector would work, try to identify the locator..:) – Saurabh Gaur Feb 15 '17 at 08:00
  • I've tried with ID and CSS path but its not working. – ajay kumar Feb 15 '17 at 08:07
  • @ajaykumar then you can ask another question regarding this.. Thanks..:) – Saurabh Gaur Feb 15 '17 at 08:09
  • Thank you, so the scenario is: 1. Without selecting client we cannot select facility as its disabled. 2. We've selected client value from drop down. 3. Now I've written a script for a new tab. 4. After that, I've to find facility field through ID but it shows element is not found, then could you please help me for the same? – ajay kumar Feb 15 '17 at 08:16
  • @ajaykumar post another question for this.. Thanks..:) – Saurabh Gaur Feb 15 '17 at 09:32
  • Done Thanks Saurabh – ajay kumar Feb 15 '17 at 09:44
  • @ajaykumar If this answer helped, [mark this answer as correct](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)..:) – Saurabh Gaur Feb 15 '17 at 10:53
1

Below approach might be helpful:

// Enter text in auto complete text box
driver.findElement(By.xpath("//*[@id='client']")).sendKeys("Ho");

// Wait for options to display
Thread.sleep(5000);

// Option to select
String optionToSelect = "Honda";

Boolean isOptionSelected = Boolean.FALSE;

// Get the options displayed
List<WebElement> options = driver.findElements(By
        .cssSelector("ul.dropdown-menu a"));

// Select option
for (WebElement webElement : options) {
    if (webElement.getText().equalsIgnoreCase(optionToSelect)) {
        webElement.click();
        isOptionSelected = Boolean.TRUE;
    }
}

if (isOptionSelected) {
    // Option is selected
} else {
    // Expected option is not displayed. Fail the script
}
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Prudhvi U
  • 9
  • 3
0

Try this:

Select drpCountry = new Select(driver.findElement(By.id("searchOptions")));
drpCountry.selectByVisibleText("By author");
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Priya
  • 1
  • 1