javaseleniumselenium-webdriverxpathtestng

Unable to select option from drop down(Tried all ways) in selenium


I am trying to select the option from the drop down menu. But it is not getting selected. The test case passes without any error and without selecting the option. Since it is HTML drop down I have used click. I tried Select class but it did not work. The site is https://demoqa.com/automation-practice-form/ The code I have written here is

>     JavascriptExecutor js=(JavascriptExecutor)driver;
>     Actions act=new Actions(driver);
>     js.executeScript("window.scrollBy(0,500)");
>     WebElement we=driver.findElement(By.xpath("//div[@id='state']"));
>     act.moveToElement(we).click().build().perform();
>     WebElement we3=driver.findElement(By.xpath("//div[contains(.,'Uttar
> Pradesh')]/following-sibling::div/descendant::input"));
        act.moveToElement(we3).click(we3).build().perform();

your help is solicited. Thanks

enter image description here

enter image description here


Solution

  • Its different element u need to click after sending keys as "Uttar Pradesh"

    Use below code

    new WebDriverWait(driver , 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='react-select-3-input']"))).sendKeys("Uttar");
    new WebDriverWait(driver ,20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(@id,'react-select')]"))).click();