已更新 在我的 Django 设置中,当我将 DEBUG 设置为 false 时,Selenium 能够与元素交互。当DEBUG设置为true时仍然不起作用
因此,我尝试在下拉菜单中选择一个选项,但是当我尝试直接访问 <select>
时标签我收到错误 ElementNotVisibleException: Message: element not interactable
.
在控制台中,每当我单击下拉列表时,仅指根据我单击的内容动态更改的输入标记。最终,我希望测试单击下拉菜单,然后选择“Tango”,如下面的链接所示。
我的 Selenium 代码是:
from selenium.webdriver import Chrome
from selenium.webdriver.support.ui import Select
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("localhost:8000/")
select = Select(driver.find_element_by_xpath('//*[@id="select-dance"]').click())
select.select_by_value('1')
和 HTML:
<form onsubmit="return false;">
<div class="row border-light bg-white rounded-left rounded-right no-gutters">
<div class="col-12 col-lg px-3">
<div class="select-wrapper mdb-select">
<span class="caret"></span>
<input type="text" class="select-dropdown" readonly="true" data-activates="select-options-20f378f1-9560-4598-b8e8-
3ffe496cd688" value="Choose your dance event">
<ul id="select-options-20f378f1-9560-4598-b8e8-3ffe496cd688" class="dropdown-content select-dropdown" style="width: 658px; position:absolute;top: 0px; left: 0px;opacity: 1; display: none;">
<li class="">
<span class="filtrable">Choose your dance event</span></li>
<li class="">
<span class="filtrable">Tango</span></li>
<li class="active selected">
<span class="filtrable">Swing</span>
</li>
<li class="">
<span class="filtrable">Latin/Salsa</span>
</li>
<li class="">
<span class="filtrable">Ballroom</span>
</li>
<li class="">
<span class="filtrable">Bachata</span>
</li>
</ul>
<select class="mdb-select initialized" id="select-dance">
<option value="0">Choose your dance event</option>
<option value="1">Tango</option>
<option value="2">Swing</option>
<option value="3">Latin/Salsa</option>
<option value="4">Ballroom</option>
<option value="5">Bachata</option>
</select>
</div>
</div>
</div>
</div>
</form>
我不知道如何处理干扰选择的输入标签(它只显示在Chrome的元素控制台中)。 谢谢!
最佳答案
你可以处理,
driver = webdriver.Chrome()
driver.get("localhost:8000/")
time.sleep(10)
select = Select(driver.find_element_by_id('select-dance'))
select.select_by_value('1')
关于python - 由于可见性问题,Selenium 将不会选择下拉选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52771960/