Select an option using python from select2 and fill web form

谁说胖子不能爱 提交于 2021-02-08 10:09:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!