P粉6654279882023-09-05 11:06:00
There seems to be something wrong with your HTML structure to me. An input
element does not contain options
; a select
element does. My code snippet shows you how to programmatically assign the value of the select
dropdown menu to the second option, which is APPLE.
It also uses your existing structure (derived from your query), but the last element no longer uses input
but select
because semantically Speaking of which, input
and options
are not logical. So, if my explanation is wrong, my solution may not accurately answer your question. But I hope it still guides you in the right direction.
const select = document.querySelector(".ng-touched > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > select:nth-child(1)") // setTimeout used to visualize how GOOGLE changes to APPLE. setTimeout(() => { // You can access the options of the select with .options. // Then you can programatically set the selected value by index. select.options.selectedIndex = 1 }, 2000)
<div class="ng-touched"> <div style="padding-left: 16px;"> <div style="padding-left: 16px;"> <div> </div> <div style="padding-left: 16px;"> <select id="companies"> <option value="google">GOOGLE</option> <option value="apple">APPLE</option> <option value="facebook">FACEBOOK</option> <option value="amazon">AMAZON</option> </select> </div> </div> </div> </div>