selenium python动态下拉菜单

selenium python动态下拉菜单,python,selenium,Python,Selenium,我想从一个网站自动填写表格,一切顺利,直到我需要从动态下拉菜单中选择一个选项 <div id="field_type_group" class="field trigger-option group0" style=""> <div class="label"><label>Input Type</label></div> <div class="input"> <select> &l

我想从一个网站自动填写表格,一切顺利,直到我需要从动态下拉菜单中选择一个选项

<div id="field_type_group" class="field trigger-option group0" style="">
  <div class="label"><label>Input Type</label></div>
  <div class="input">
    <select>
      <option data-item-id="100" value="text_field_validation_type">Text input</option>
      <option data-item-id="200" value="dropdown_field_type">Dropdown list</option>
      <option data-item-id="300" value="location_field_type">Location based input</option>
    </select>
  </div>
</div>

<div id="text_field_validation_type" class="field trigger-option group100" style="display: none;">
  <div class="label"><label>Input validation</label></div>
  <div class="input">
    <select>
      <option data-item-id="0" data-displayfield="text_raw" value="none">No validation required</option>
      <option data-item-id="500" value="text_field_type">Validation required</option>
    </select>
  </div>
</div>

<div id="dropdown_field_type" class="field trigger-option group200" style="">
  <div class="label"><label>Data source for dropdown list</label></div>
  <div class="input">
    <select>
      <option data-item-id="6" data-displayfield="dropdown_static" value="none">Populate with list items specified here</option>
      <option data-item-id="7" data-displayfield="dropdown_dynamic" value="none">Retrieve list items from my service</option>
    </select>
  </div>
</div>
但是当我需要选择子菜单时,问题就会出现

我尝试过很多方法(也尝试过使用Select和ActionChains),但都没有效果

我的代码使用选择:

select_element = browser.find_element_by_xpath("//select[option[@value='text_field_validation_type']]")
select = Select(select_element)
select.select_by_index(1)

select_element = browser.find_element_by_xpath("//select[option[@data-item-id='6']]")
select = Select(select_element)
select.select_by_index(1)
我的其他代码:

wait.until(ec.visibility_of_element_located((By.XPATH, "//select[option[@data-item-id ='6']]"))).click()
browser.find_element_by_xpath(".//option[@data-displayfield='dropdown_dynamic']").click()
我尝试了许多方法来更改xpath值,但都不起作用,而且我的环境不支持ActionChains

编辑:

我发现我需要先点击下拉菜单,然后我可以使用选择功能,但它仍然只能在第一个菜单上工作。这是代码

wait.until(ec.visibility_of_element_located((By.XPATH, "//select[option[@value='text_field_validation_type']]"))).click()
sleep(2)
select_element = browser.find_element_by_xpath("//select[option[@value='dropdown_field_type']]")
select = Select(select_element)
#select.select_by_index(1)
select.select_by_visible_text('Dropdown list')
我还注意到,在动态下拉子菜单上,他们使用另一个div和
style=“display:none;”“
当它不是我所选菜单的下拉子菜单时,是否影响了我的问题?我添加了一点HTML菜单


有人能帮我吗?非常感谢。

使用Selenium尝试下面的代码
选择基于标签的xpath,然后等待元素可单击:

sub_menu = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@id='dropdown_field_type' and .//label[.='Data source for dropdown list']]//select")))
select = Select(sub_menu)
select.select_by_index(0)
# or
select.select_by_visible_text("Populate with list items specified here")

使用Selenium
选择基于标签的xpath并等待元素可单击,尝试下面的代码:

sub_menu = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@id='dropdown_field_type' and .//label[.='Data source for dropdown list']]//select")))
select = Select(sub_menu)
select.select_by_index(0)
# or
select.select_by_visible_text("Populate with list items specified here")

您可以尝试这样使用xpath吗

dd1 = "//*[text() = 'Input Type']//following::select[1]"
dd2 = "//*[contains(text() , 'Data source')]//following::select[1]"
按文本或值选择第一个下拉列表

select = Select(driver.find_element_by_xpath(dd1))

# select by visible text
select.select_by_visible_text('Dropdown list')
# OR select by value 
select.select_by_value('dropdown_field_type')
然后选择第二个下拉列表

select = Select(driver.find_element_by_xpath(dd2))
# select by visible text
select.select_by_visible_text('Populate with list items specified here')

请确保添加大小写正确的文本。

您可以尝试这样使用xpath吗

dd1 = "//*[text() = 'Input Type']//following::select[1]"
dd2 = "//*[contains(text() , 'Data source')]//following::select[1]"
按文本或值选择第一个下拉列表

select = Select(driver.find_element_by_xpath(dd1))

# select by visible text
select.select_by_visible_text('Dropdown list')
# OR select by value 
select.select_by_value('dropdown_field_type')
然后选择第二个下拉列表

select = Select(driver.find_element_by_xpath(dd2))
# select by visible text
select.select_by_visible_text('Populate with list items specified here')


确保使用正确的大小写添加准确的文本。

尝试了许多方法后,解决方案是升级web浏览器并使用最新的geckodriver,然后使用选择功能。我的错。非常感谢您的帮助。

在尝试了许多方法之后,解决方案是升级web浏览器并使用最新的geckodriver,然后使用Select功能。我的错。非常感谢您的帮助。

与“菜单”和“子菜单”共享HTML,您的代码我已经编辑过了。在第一个下拉列表中选择选项后,“子菜单”下拉列表是否会刷新?如果是这种情况,您可能需要等待一段时间。您遇到了什么错误?@Sers我没有收到任何错误,该值只是没有更改与菜单和子菜单共享HTML,并且您为它们编写的代码我已对其进行了编辑。在您选择第一个下拉菜单中的选项后,子菜单下拉菜单是否会刷新?如果是这样的话,你可能需要等待一段时间。你得到了什么错误?@Sers我没有得到任何错误,值只是没有改变。顺便说一句,你的代码在我第一次单击菜单后也适用于菜单,但在子菜单中不适用,你知道为什么吗。顺便说一句,你的代码在我第一次单击菜单后也适用于菜单,但是子菜单不起作用,你知道为什么吗。你能共享URL吗?在我更新浏览器并使用最新的geckodriver后,它工作得很好。很抱歉。我的坏。你能分享URL吗?在我更新浏览器并使用最新的geckodriver后,它运行良好。很抱歉。我的错。