由于可见性问题,Selenium将不会选择下拉选项

时间:2018-10-12 03:47:32

标签: python selenium selenium-webdriver webdriver selenium-chromedriver

已更新 在Django设置中,当我将DEBUG设置为false时,Selenium能够与元素进行交互。当DEBUG设置为true时,它仍然不起作用

因此,我尝试在下拉菜单中选择一个选项,但是当我尝试直接访问<select>标签时,出现错误ElementNotVisibleException: Message: element not interactable

在控制台中,每当我单击下拉菜单时,仅指根据我单击的内容动态更改的输入标签。最终,我希望测试单击下拉列表,然后选择“ Tango”,如下面的链接所示。

click here

我的硒代码为:

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的元素控制台中)。 谢谢!

3 个答案:

答案 0 :(得分:0)

您可以通过

driver = webdriver.Chrome()
driver.get("localhost:8000/")

time.sleep(10)
select = Select(driver.find_element_by_id('select-dance'))
select.select_by_value('1')

答案 1 :(得分:0)

您可以尝试一下,

driver = webdriver.Chrome()
driver.get("localhost:8000/")

time.sleep(10)
driver.find_element_by_id('select-dance').click()

当您单击时,将在下填充的新列表,然后调用select标记

select = Select(driver.find_element_by_id('select-dance'))
select.select_by_value('1')

select = Select(driver.find_element_by_xpath('//*[@id="select-dance"]'))
select.select_by_value('1')

答案 2 :(得分:0)

您的页面使用的自定义选择组件不是默认的html select box。就您而言,他们使用的MDBootstrap Select Component不能与硒Select类进行交互。

请阅读this答案,以回答与同一问题相关的另一个问题。