0

I'm trying to write Selenium code for below HTML source code..

This field is the auto populated field for input selection

<input id="ctl00_ContentPlaceHolder1_txtBranch" class="textbox_service ui-autocomplete-input" name="ctl00$ContentPlaceHolder1$txtBranch" style="width: 200px;" onblur="return branch();" onchange="return CheckBranchName();" tabindex="6" autocomplete="off" type="text"/>

Any one can help me out to write the code?

Web element screen shot attached. Thanks in advance.

Adi
  • 9
  • 2
  • Possible duplicate of [Selecting options from Auto Complete Dropdown using selenium web driver in python](https://stackoverflow.com/questions/45616513/selecting-options-from-auto-complete-dropdown-using-selenium-web-driver-in-pytho) – undetected Selenium Dec 08 '17 at 05:59

2 Answers2

0

This is the best I could do with the information you provided. If you could show the HTML for what the autocomplete list looks like that would be great. You didn't specify any language so I'll assume it's Java.

WebElement field = driver.findElement(By.id("ctl00_ContentPlaceHolder1_txtBranch"));
field.click();
field.sendKeys(Keys.SPACE);

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

for (int i=0; i<items.size();i++) {
WebElement elementYouWantToClick = items.get(i);
     String x = elementYouWantToClick.getText();
     if(x.contains("TextThatIsInYourElementYouWantToChoose")){
         elementYouWantToClick.click();
}

Best I could do for now with such limited information.

Reez0
  • 2,512
  • 2
  • 17
  • 39
  • Hi Thanks for code., When i am trying with bit change of your code drop down is displaying but item not able to select with code {driver.switchTo().frame(0); WebElement field = driver.findElement(By.id("ctl00_ContentPlaceHolder1_txtBranch")); field.click(); field.sendKeys(Keys.SPACE); Thread.sleep(3000); driver.switchTo().defaultContent(); driver.switchTo().frame(0); List items = field.findElements(By.tagName("li")); for (WebElement options1 : items) { if("MEHMDAVAD(DSH001)|1".equals(options1.getText())) { options1.click(); } } – Adi Dec 12 '17 at 09:24
  • What exception are you getting? – Reez0 Dec 12 '17 at 09:32
0

As per the HTML to click(select) the Auto Complete text, you can use the following line of code :

new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='textbox_service ui-autocomplete-input' and contains(@id,'_ContentPlaceHolder') and contains(@name,'txtBranch')]"))).click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352