Dynamic Dropdown Selection Issue

When attempting to select the departure city on the Rahul Shetty Academy Website using the Selenium Web Driver, the departure city selected using XPath isn’t selected. My code is below the asterisks and I receive the following error within the asterisks.

Exception:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@selectedvalue="BLR"]"}
      (Session info: chrome=121.0.6167.85)
    For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
    Build info: version: '4.17.0', revision: 'e52b1be057*'
    System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '14.2.1', java.version: '17.0.9'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Command: [f9e14578261c798b2021551ffe86d360, findElement {value=//input[@selectedvalue="BLR"], using=xpath}]
    Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 121.0.6167.85, chrome: {chromedriverVersion: 121.0.6167.85 (3f98d690ad7e..., userDataDir: /var/folders/md/pyls622d4kd...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:61894}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: mac, proxy: Proxy(), se:cdp: ws://localhost:61894/devtoo..., se:cdpVersion: 121.0.6167.85, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
    Session ID: f9e14578261c798b2021551ffe86d360
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
        at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185)
        at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:134)
        at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:51)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:190)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:216)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:174)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:519)
        at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:165)
        at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:59)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:356)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:350)
        at DynamicDropdowns.main(DynamicDropdowns.java:19)

DynamicsDropdown Class:

import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;



    public class DynamicDropdowns {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver",         "/Users/mfigures/Documents/COFTA/chromedriver-mac-x64/chromedriver");
        
        WebDriver driver = new ChromeDriver();
        driver.get("https://rahulshettyacademy.com/dropdownsPractise/");
        
        //input[@selectedvalue="BLR"]
        //input[@selectedvalue="MAA"]
        driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();
        Thread.sleep(2000);
        
        driver.findElement(By.xpath("//input[@selectedvalue="BLR"]")).click();
        driver.findElement(By.xpath("//input[@selectedvalue="MAA"]")).click();
    
    }
}

  • First, you should use an xpath to find the “select” input because there are 3 elements with the same id. Then it seems that your xpath for the selected value is wrong, what you are searching is in this div : //div[@id=”dropdownGroup1″]/div But i don’t understand why are u making two clicks whereas we can’t select two values on the select

    – 




Your dropdown containers are rendered after some time after click.
You need add waits for render conditions.

To correctly handle this case, you need to wait for dropdown options presence, then you need to click on it, wait for it’s invisibility, get dropdown options again and filter for visible one.

WebDriverWait wdwait = new WebDriverWait(driver, 10);
driver.get("https://rahulshettyacademy.com/dropdownsPractise/");
wdwait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("ctl00_mainContent_ddl_originStation1_CTXT")))
                .get(0)
                .click();
WebElement from = wdwait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#dropdownGroup1 [value=BLR]")));
from.click();
wdwait.until(ExpectedConditions.invisibilityOf(from));
WebElement to = wdwait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("#dropdownGroup1 [value=MAA]")))
                .stream()
                .filter(WebElement::isDisplayed)
                .findFirst()
                .get();
to.click();

Leave a Comment