3

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?

  • Is it possible to share url ? – cruisepandey Aug 17 '21 at 15:53
  • 1
    @cruisepandey Try to [add any item](https://www.amazon.com/dp/B01BKKQ7ZK) to your Amazon Shopping Cart, navigate to [Shopping Cart Page](https://www.amazon.com/gp/cart/view.html) and try updating Qty from the default value to custom value (3, 4, 5 etc). Thanks. – Vance Brockberg Aug 17 '21 at 16:30
  • If possible can you please share actual url? For Amazon, the qty options on cart page fall under "ul" tag and not "select" tag, hence can't be used in the JS code you have shared. Additionally, the JS code works fine for a normal drop down element without any overlay element. – Kundan Aug 17 '21 at 17:46
  • @Kundan I encountered this issue with several URLs. The one I'm working on now requires authorization so I cannot share it but the example from Amazon is open to the public. Just add any product to your shopping cart, go the Shopping Cart Page and try updating the quantity. The options are actually behind both "select" & "ul".. See snapshot of HTML -> ` – Vance Brockberg Aug 17 '21 at 23:16
  • @Kundan Not sure why Amazon does this but it also looks like they're reloading the page via AJAX after clicking quantity. At the same time, you can see that there's a select tag associated with quantity drop down. I'm not sure why they would need both 'ul' and 'select'. – Vance Brockberg Aug 18 '21 at 00:34

0 Answers0