硒python动态下拉菜单

时间:2019-10-26 11:54:53

标签: 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>
      <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菜单。

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

3 个答案:

答案 0 :(得分:1)

尝试以下使用硒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")

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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