简体   繁体   中英

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 ");

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:

印度出生国家

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM