How to select an option from auto suggestions using Selenium and Java?

I have these two options in custom dropdown – British Indian Territory and India. Using selenium sendKeys when I am entering “India”, it is not able to enter India in the respective country field, since it is unable to differentiate between British ‘India’n Territory and India values in the dropdown.

Link to the website:
https://www.westernunion.com/gb/en/send-money/app/register

WebElement country = driver.findElement(By.xpath("//input[@id="countryOfBirthField"]"));

country.clear();
country.sendKeys(" india ");

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Among the two dropdown options as British Indian Territory and India to select India in the respective country field you can use the following solution: Code Block: public class A_demo { public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("start-maximized"); options.addArguments("disable-infobars"); WebDriver driver = new ChromeDriver(options); driver.get("https://www.westernunion.com/gb/en/send-money/app/register"); WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#countryOfBirthField"))); element.sendKeys("india"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='dropdown-menu ng-isolate-scope']//li/a[@title='India']/strong[text()='India']"))).click(); } } Browser Snapshot:

Jason
Guest

This is another approach to achieve

driver.get(“https://www.westernunion.com/gb/en/send-money/app/register“);
dropdownstackoverflow=commonMethod.getWebelement(driver, bdropdownstackoverflow);
dropdownstackoverflow.sendKeys(“India”);

Robot r=new Robot();

r.keyPress(KeyEvent.VK_DOWN);
r.keyRelease(KeyEvent.VK_DOWN);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);