Stack Exchange
Selecting dropdown value without ID using Selenium
Got below HTMLcode for dropdown:
<select name="PWCMasterPage$PWCWebPartManager$gwpTemplateFr1$TemplateFr1$drpProductType" id="PWCMasterPage_PWCWebPartManager_gwpTemplateFr1_TemplateFr1_drpProductType" tabindex="2" class="PWCDropDownList" profiledatamember="" profileid="TEMPLATE" onblur="this.Holder = GetControlHolder(this);" onchange="this.Holder = GetControlHolder(this);" onfocus="this.Holder = GetControlHolder(this);" data-val-subtype-type="none" controlscollectionname="TemplateFr1_drpProductType" data-configid="TemplateFr1_drpProductType" holdername="TemplateFr1Holder">
<option value=""></option>
<option value="7">Expedited</option>
<option value="8">Premier</option>
<option value="9">Value</option>
</select>

Have been trying to select the dropdown values with:
Select dropDown = new Select(driver.findElement(By.xpath("//*[@id=\"PWCMasterPage_PWCWebPartManager_gwpTemplateFr1_TemplateFr1_drpProductType\"]")));
dropDown.selectByValue("8");

Getting below error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Cannot locate option with value: 8

Select dropDown = new Select(driver.findElement(By.xpath("//*[@id=\"PWCMasterPage_PWCWebPartManager_gwpTemplateFr1_TemplateFr1_drpProductType\"]")));
dropDown.selectByVisibleText("Expedited");

Which resulted in below error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Cannot locate element with text: Expedited

Have tried alternately with selectByVisibleText and selectByIndex as well which resulted in similar errors.

Can you check if an iframe is present on the page?

@Sameer How can I check that?

Inspect that element and in the html page source, check above that element if a html tag iframe is present anywhere above it.

@Sree Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace. Sameer Arora

@SameerArora Have checked there is no iframe present.

Just posted an answer but it's based on info you've not disclosed - can you please share versions you're using?  (browser, driver and selenium)  - if it helps, the code looks fine!

am using Chrome browser.

The issue isn't the java code you provided or the HTML source.
I created a simple web page with your html:
<html><body>
<select name="PWCMasterPage$PWCWebPartManager$gwpTemplateFr1$TemplateFr1$drpProductType" id="PWCMasterPage_PWCWebPartManager_gwpTemplateFr1_TemplateFr1_drpProductType" tabindex="2" class="PWCDropDownList" profiledatamember="" profileid="TEMPLATE" onblur="this.Holder = GetControlHolder(this);" onchange="this.Holder = GetControlHolder(this);" onfocus="this.Holder = GetControlHolder(this);" data-val-subtype-type="none" controlscollectionname="TemplateFr1_drpProductType" data-configid="TemplateFr1_drpProductType" holdername="TemplateFr1Holder">
    <option value=""></option>
    <option value="7">Expedited</option>
    <option value="8">Premier</option>
    <option value="9">Value</option>
</select>
</body></html>

I created a simple test class - and it works lovely!
Pasting the lot so you can see i do nothing other than open the page, use your code and select the element.

public class StackTest {

    private String baseUrl = "C:\\Git\\stackTest.html";
    private WebDriver driver;

    @Before
    public void CreateWebDriver()
    {
        System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get(baseUrl);
    }

    @After
    public void CloseAndQuitWebDriver() {
        driver.close();
        driver.quit();
    }

    @Test
    public void approach1_ExistingCode()
    {
        Select dropDown = new Select(driver.findElement(By.xpath("//*[@id=\"PWCMasterPage_PWCWebPartManager_gwpTemplateFr1_TemplateFr1_drpProductType\"]")));
        dropDown.selectByValue("8");
    }
}


In my opinion, the problem isn't selenium, the problem isn't the xpath, as long as you're doing what @SeleniumUser002 says in his answer then the problem isn't object accessibility.
Have a think about:

What is your site doing that's special?
Is it doing anything script based which creates/populates this drop down at run time?
When you captured the html did you capture it from chrome manually or chromedriver at runtime?  (try the latter if you didn't already  - Running manually is what a user sees, but chromedriver is what selenium sees. Be careful not to click on anything else other than the object when inspecting so you don't skew the results)
Are you doing any other steps in the script that can influence this dropdown?

First thing i would say to really fixing this issue is to use a breakpoint before your broken line and slowly step though the actions that lead the issue. See if the page is behaving differently at runtime. Walking through slowly also removes synchronisation issues and once the code works once, if it goes back to being flaky you can rule out the code and focus on a better wait strategy.
If that doesn't work and you're STILL seeing all the options present but cannot select, do a dump of what selenium can see:
@Test
public void debug_WhatAreMyOptions()
    {
        Select dropDown = new Select(driver.findElement(dropdownIdentifier));
        System.out.println("dropdown has "+ dropDown.getOptions().size()+" options");

        System.out.println("the options are....");
        for (var dropdownOptions : dropDown.getOptions()) {
            System.out.println(dropdownOptions.getText());
        }

    }

That might help you actually figure out what's happening to solve the issue.
Without that debugging information no one can tell you what the problem is - but i can suggest work arounds!
Try these...
Set this up:
private By dropdownIdentifier = By.xpath("//*[@id=\"PWCMasterPage_PWCWebPartManager_gwpTemplateFr1_TemplateFr1_drpProductType\"]");
    

Then try old fashioned clicks without a select object:
public void approach2_Clicks()
    {
        driver.findElement(dropdownIdentifier ).click(); // to activate
        driver.findElement(dropdownIdentifier ).findElement(By.xpath("//*[@value='8']")).click();
    }

Or try our old friend javascript:
 public void approach3_Javascript()
    {
        var dropdown = driver.findElement(dropdownIdentifier);
        ((JavascriptExecutor) driver).executeScript("arguments[0].value='8'", dropdown);
    }

However - these work arounds are only possible if the list is populated... My gut tells me that the data isn't there to select at runtime, but as you say it's an internal link so only you can verify that :-)
Give it a go and let us know.

yes you were correct, the dropdown list is getting binded at run time only after clicking on dropdown

My gut is rarely wrong! :-)    -  So, one of the options above is to click the object first. Click it, then probably add a wait until it's populated. but if this answer works for you mark it as right so other folks can learn then job done

you are talking about approach2_Clicks(), is that correct?

Hi Sree -try them all. Now you've got to the root of the problem (the list doesn't populate before a click) even your original code might work. You need to do that action to populate the list before you select.  you might first approach2 will work, but you also might need to put a wait in there too depending on how the data is loaded and how long it takes   ....  best thing to do is try it and see.

driver.findElement(dropdown).click(); // to activate
Error: The method findElement(By) in the type WebDriver is not applicable for the arguments (Select)

@Sree - this works for me in the html source you provided.. Have you set private By dropdownIdentifier = By.xpath("//*[@id=\"PWCMasterPage_PWCWebPartManager_gwpTemplateFr1_TemplateFr1_drpProductType\"]");  in the same class?

Let us continue this discussion in chat.

I see it now - i renamed the variable after i added bits in here - it's  driver.findElement(dropdownIdentifier) ...etc...  make it match that identifier i posted just before  - i updated the original post

Are you using safari?
If so, there's known issues with webdriver and safari. The long and short of it is to use the driver provided by apple.
This is the selenium issue: https://github.com/SeleniumHQ/selenium/issues/3145
If you look at:
https://webkit.org/blog/6900/webdriver-support-in-safari-10/
They say:

"Safari’s driver is launchable via the /usr/bin/safaridriver
executable, and most client libraries provided by Selenium will
automatically launch the driver this way without further
configuration."


I'm using chrome

nevermind then :-)  you didn't say which browser so it was a guess. Can you share you versions please?

Chrome Version 83.0.4103.116,
Selenium Standalone Server: selenium-server-standalone-3.141.59.jar
chromedriver_win32

how about chromedriver version?  -and,  are you using the alpha of selenium4? or are you using selenium 3?

Is there a command with which I can know this, if I look at the downloaded file I'm not sure

Does that mean you're not using maven or gradle as dependency manager?  -  either way, when you downloaded the libraries you would have downloaded a version to match your chrome....

Let us continue this discussion in chat.

try below solution with WebDriverWait to avoid synchronization issue:
    WebDriverWait wait = new WebDriverWait(driver,30);
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("PWCDropDownList")));
    Select drpCountry = new Select(element);
    drpCountry.selectByValue("8");


Have done this already, it did not work. Similar error was reported:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Cannot locate option with value: 8

can you please share your url ? and also check if your element not present within iframe if so you need to switch to iframe before handling your ddrop down element

It is internal url

The method visibilityOfElementLocated(By) in the type ExpectedConditions is not applicable for the arguments (Select)

Let us continue this discussion in chat.
