问题
I have this html code from select2 script:
<div class='round10'>
<select class='select2-authors-multiple' name='author[]' id='author' multiple style='width: 100%;'></select>
and this from same page:
<div class='round10'>
<select class='select2-narrators-muliple' name='narrator[]' id='narrator' multiple='multiple' style='width: 100%;'></select>
From firefox inspector i get this:
<input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="none" spellcheck="false" role="searchbox" aria-autocomplete="list" placeholder="Select an Author" style="width: 1251.9px;">
<input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="none" spellcheck="false" role="searchbox" aria-autocomplete="list" placeholder="Select a Narrator" style="width: 1241.9px;">
I have made this code and it works:
driver.execute_script("$('.select2-search__field').val('example').trigger('change')")
but if fill both of forms with "example", how can i separate them? how to ad an additional attribute to the jQuery selector to further limit the matching elements?
try to use this to identify them but it did not work:
# author
script = "$('.select2-search__field[id='Author']').val('example').trigger('change'))"
# generate a script via javascript
driver.execute_script(script)
回答1:
try to change one class of input field
class="select2-search__field1"
like this
<input class="select2-search__field1" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="none" spellcheck="false" role="searchbox" aria-autocomplete="list" placeholder="Select a Narrator" style="width: 1241.9px;">
来源:https://stackoverflow.com/questions/65727450/select-an-option-using-python-from-select2-and-fill-web-form