【问题标题】:selenium python dynamic dropdown menuselenium python动态下拉菜单
【发布时间】:2020-02-22 12:21:30
【问题描述】:

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

<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>

下拉菜单是这样的,在尝试了很多方法后,我可以成功地将它选为第一个菜单。

wait.until(ec.visibility_of_element_located((By.XPATH, "//select[option[@value='text_field_validation_type']]"))).click()
browser.find_element_by_xpath(".//option[text()[contains(.,'Dropdown list')]]").click()

但是当我需要选择子菜单时出现问题。

我尝试了很多方法(也尝试使用 Select 和 ActionChains),但没有任何效果。

我使用 Select 的代码:

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')

我还注意到,在动态下拉子菜单上,当它不是我选择的菜单的下拉子菜单时,他们使用另一个带有style="display:none;" 的 div,这会影响我的问题吗?我添加了一点 HTML 菜单。

有人可以帮助我吗?非常感谢。

【问题讨论】:

  • 与菜单和子菜单共享 HTML,以及您的代码
  • 我已经编辑过了。
  • 在第一个下拉菜单中选择一个选项后,子菜单下拉菜单是否刷新?如果是这种情况,您可能需要等待一段时间..
  • 你遇到了什么错误?
  • @Sers 我没有收到任何错误,只是值没有改变

标签: python selenium


【解决方案1】:

使用 Selenium Select 尝试下面的代码,基于标签的 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")

【讨论】:

  • 可以分享网址吗?
  • 在我更新浏览器并使用最新的 geckodriver 后效果很好。对于那个很抱歉。我的错。
【解决方案2】:

您可以尝试像这样使用 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')

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

【讨论】:

  • 顺便说一句,您的代码在我先单击后也适用于菜单,但不适用于子菜单,您知道原因吗?
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 2016-11-12
    • 2019-04-05
    • 2023-03-25
    • 1970-01-01
    • 2022-11-11
    相关资源
    最近更新 更多