Home > database >  How do I click on this autoComplete countryName?
How do I click on this autoComplete countryName?

Time:09-05

This is the link I wanted to automate. But I am not being able to click on countryName. I wanted to enter "na" in the searchField and then click on "Nepal" using the list attribute i.e list.get(i).click() but I have not been able to. Please help

package autoComplete;
    import java.time.Duration;
    import java.util.List;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.interactions.Actions;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    import io.github.bonigarcia.wdm.WebDriverManager;

public class AutoCompleteCountry {
    WebDriver driver;
    String url = "https://practice-cybertekschool.herokuapp.com/autocomplete";
    By countryField = By.id("myCountry");
    By countryList = By.xpath("//input[@type='hidden']");

    @BeforeTest
    public void getUrl() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));

        driver.get(url);
    }

    @Test
    public void autoCompleteTest() throws InterruptedException {
        driver.findElement(countryField).sendKeys("N");
        List<WebElement> listOfCountry = driver.findElements(countryList);
        // driver.findElement(nepalClick).click();

        for (int i = 0; i < listOfCountry.size(); i  ) {
            // System.out.println(list);
            String searchText = listOfCountry.get(i).getAttribute("value");
            System.out.println(searchText);
            if (searchText.equals("Nepal")) {
                listOfCountry.get(i).click();
            }
        }

    }

    @AfterTest
    public void tearDown() throws InterruptedException {
        Thread.sleep(5000);
        driver.quit();
    }

}

This is the error screenshot: Error Screenshot

CodePudding user response:

You needn't iterate through the whole list if you just want to click the "Nepal" option. Does the following help at all?

driver.findElement(By.CSS_SELECTOR, 'input[value="Nepal"]').click()

CodePudding user response:

you can use Webdriver wait with a customExpected Condition like this below Instead of using implicit wait use WebDriverwait for Fluentwait

private static ExpectedCondition<Boolean> waitForDropdownElement(By by, String value) {
      return  ( driver -> {
         List<WebElement> listOfCountry = driver.findElements(by);
         for (int i = 0; i < listOfCountry.size(); i  ) {
            String searchText = listOfCountry.get(i).getText();
            if (searchText.equals(value)) {
               listOfCountry.get(i).click();
               return true;
            }
         }
         return false;
      });
   }

Also modified below locator like this, as you will get element not clickable exception on using that

By countryList = By.xpath("//div[@id='myCountryautocomplete-list']//div");

Full code below which is working for me

import io.github.bonigarcia.wdm.WebDriverManager;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AutoCompleteCountry {

   WebDriver driver;
   String url = "https://practice-cybertekschool.herokuapp.com/autocomplete";
   By countryField = By.id("myCountry");
   By countryList = By.xpath("//div[@id='myCountryautocomplete-list']//div");

   @BeforeTest
   public void getUrl() {
      WebDriverManager.chromedriver().setup();
      driver = new ChromeDriver();
      driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
      driver.get(url);
   }

   @Test
   public void autoCompleteTest() throws InterruptedException {
      driver.findElement(countryField).sendKeys("N");
      WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(60));
      wait.until(waitForDropdownElement(countryList, "Nepal"));

   }

   @AfterTest
   public void tearDown() throws InterruptedException {
      Thread.sleep(5000);
      driver.quit();
   }

   private static ExpectedCondition<Boolean> waitForDropdownElement(By by, String value) {
      return (driver -> {
         List<WebElement> listOfCountry = driver.findElements(by);
         for (int i = 0; i < listOfCountry.size(); i  ) {
            String searchText = listOfCountry.get(i).getText();
            if (searchText.equals(value)) {
               listOfCountry.get(i).click();
               return true;
            }
         }
         return false;
      });
   }
}
  • Related