Unable to select a dropdown by value, visible text or index using selenium select command
<p>I am unable to select an option dynamically using select commands by value, visible text or index, the workaround was i can use actions to click select and select a particular option or sending keys ArrowDown which is not a best way to implement because i want to use the same select function globally to automate UI all over the website like a keyword function. </p>

<p>sample script below:</p>

<pre><code>WebElement selectElement = driver.findElement(By.xpath(.//*[@id='XYZPopUpForm:j_idt90:country_label']));

Actions selectItem = new Actions(driver);
selectItem.moveToElement(selectElement).click().perform();  
selectItem.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).perform();
selectItem.sendKeys(Keys.ENTER).perform();
</code></pre>

<p>HTML Code :</p>

<pre><code>&lt;tr&gt;
&lt;td&gt;
&lt;label for="XYZPopUpForm:j_idt90:country"&gt;Country&lt;/label&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;div id="XYZPopUpForm:j_idt90:country" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all"&gt;
&lt;div class="ui-helper-hidden-accessible"&gt;
&lt;div class="ui-helper-hidden-accessible"&gt;
&lt;select id="XYZPopUpForm:j_idt90:country_input" tabindex="-1" name="XYZPopUpForm:j_idt90:country_input"&gt;
&lt;option selected="selected" disabled="disabled"&gt;--select--&lt;/option&gt;
&lt;option value="AF"&gt;Afghanistan&lt;/option&gt;
&lt;option value="AX"&gt;Åland Islands&lt;/option&gt;
&lt;option value="AL"&gt;Albania&lt;/option&gt;
&lt;option value="DZ"&gt;Algeria&lt;/option&gt;
&lt;option value="AS"&gt;American Samoa&lt;/option&gt;
&lt;option value="AD"&gt;Andorra&lt;/option&gt;
&lt;option value="AO"&gt;Angola&lt;/option&gt;
&lt;option value="AI"&gt;Anguilla&lt;/option&gt;
&lt;option value="AQ"&gt;Antarctica&lt;/option&gt;
&lt;option value="AG"&gt;Antigua and Barbuda&lt;/option&gt;
&lt;option value="AR"&gt;Argentina&lt;/option&gt;
&lt;option value="AM"&gt;Armenia&lt;/option&gt;
&lt;option value="AW"&gt;Aruba&lt;/option&gt;
&lt;option value="AU"&gt;Australia&lt;/option&gt;
&lt;option value="AT"&gt;Austria&lt;/option&gt;
&lt;option value="AZ"&gt;Azerbaijan&lt;/option&gt;
&lt;option value="BS"&gt;Bahamas&lt;/option&gt;
&lt;option value="BH"&gt;Bahrain&lt;/option&gt;
&lt;option value="BD"&gt;Bangladesh&lt;/option&gt;
&lt;option value="BB"&gt;Barbados&lt;/option&gt;
&lt;option value="BY"&gt;Belarus&lt;/option&gt;
&lt;option value="BE"&gt;Belgium&lt;/option&gt;
&lt;option value="BZ"&gt;Belize&lt;/option&gt;
&lt;option value="BJ"&gt;Benin&lt;/option&gt;
&lt;option value="BM"&gt;Bermuda&lt;/option&gt;
&lt;option value="BT"&gt;Bhutan&lt;/option&gt;
&lt;option value="BO"&gt;Bolivia, Plurinational State of&lt;/option&gt;
&lt;option value="BQ"&gt;Bonaire, Sint Eustatius and Saba&lt;/option&gt;
&lt;option value="BA"&gt;Bosnia and Herzegovina&lt;/option&gt;
&lt;option value="BW"&gt;Botswana&lt;/option&gt;
&lt;option value="BV"&gt;Bouvet Island&lt;/option&gt;
&lt;option value="BR"&gt;Brazil&lt;/option&gt;
&lt;option value="IO"&gt;British Indian Ocean Territory&lt;/option&gt;
&lt;option value="BN"&gt;Brunei Darussalam&lt;/option&gt;
&lt;option value="BG"&gt;Bulgaria&lt;/option&gt;
&lt;option value="BF"&gt;Burkina Faso&lt;/option&gt;
&lt;option value="BI"&gt;Burundi&lt;/option&gt;
&lt;option value="KH"&gt;Cambodia&lt;/option&gt;
&lt;option value="CM"&gt;Cameroon&lt;/option&gt;
&lt;option value="CA"&gt;Canada&lt;/option&gt;
&lt;option value="CV"&gt;Cape Verde&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;
</code></pre>

<p>Updated Script:</p>

<pre><code>WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement selectElement = driver.findElement(By.id("XYZPopUpForm:j_idt90:country_input"));
((JavascriptExecutor)driver).executeScript("var select = arguments[0]; for(var i = 0; i &lt; select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } } ", selectElement, "Afghanistan");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("XYZPopUpForm:j_idt90:country_input")));
</code></pre>

<p>Screenshot of HTML to be more clear :</p>

<p><a href="https://i.stack.imgur.com/67ONj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/67ONj.png" alt="https"></a></p>