3

I am trying to select the value for Subjects field in the following form: https://demoqa.com/automation-practice-form

It is an input field that dynamically gives suggestions based on our input and later we need to select values from those suggestions. I am unable to select the desired value.

The Below code only populates the input area but the value is not selected.

driver.findElement(By.id("subjectsInput")).sendKeys("English");
driver.findElement(By.id("subjectsInput")).click(); //This line doesnot click on the desired value.

How to Select the desired value. Please help.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

2

The code below worked for me.

    WebDriver Driver = new ChromeDriver();
    Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    //Driver.manage().window().maximize();
    String url = "https://demoqa.com/automation-practice-form";
    Driver.get(url);
    WebElement products=Driver.findElement(By.id("subjectsInput"));
    products.sendKeys("English");
    products.sendKeys(Keys.ARROW_DOWN);
    products.sendKeys(Keys.ENTER);
    
arpita biswas
  • 144
  • 1
  • 6
0

To invoke click on the only auto suggestion English you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    driver.get("https://demoqa.com/automation-practice-form");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#subjectsInput"))).sendKeys("English");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.subjects-auto-complete__menu"))).click();
    
  • xpath:

    driver.get("https://demoqa.com/automation-practice-form");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='subjectsInput']"))).sendKeys("English");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(@class, 'subjects-auto-complete__menu')]"))).click();
    
  • Browser Snapshot:

toolsqa_subject

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    How to inspect element with class 'subjects-auto-complete__menu' as it hides on click – Ankit Agrawal Aug 07 '20 at 12:34
  • Thanks, @DebanjanB. Now I am able to complete it. – Revanth Bangera Aug 07 '20 at 15:03
  • @DebanjanB How did you find 'subjects-auto-complete__menu' class in "//div[contains(@class, 'subjects-auto-complete__menu')]"? When I inspect the web page I dont find this class. – Revanth Bangera Aug 07 '20 at 19:13
  • @RevanthBangera Agree, it was a bit tricky as the div vanishes from the DOM when you try to click on it. But as it is a auto suggestion, the class name always gives a hint that the element with this class will contain the actual auto suggestion. – undetected Selenium Aug 07 '20 at 19:36