I'm trying to select option from a dropdown menu by visible text using Javascript Executor
:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", element, visibleText);
Although the code above does not throw an exception, the desired option is not selected (the default option remains selected). I am unable to select option using the native Selenium WebDriver Select
class because there's a permanent overlay over the element. I tried hiding the permanent overlay with JS as suggested in this response but since the targeted element is a descendant of the overlay element if I hide the overlay element it automatically hides the targeted element too.
Why isn't the JS code selecting the desired option? Is there any other way to select option from drop down via JS/Javascript Executor that might work?