python - Python / Selenium 如何与隐藏选择元素交互
问题描述
这是我需要选择特定选项的元素的 HTML 代码
<div class="ui-dropdownlistcontrol">
<select id="ApplicantTitle" name="ApplicantTitle" class="select2-hidden-accessible" data-select2-id="ApplicantTitle" tabindex="-1" aria-hidden="true">
<option value="0" selected="" data-select2-id="2"> </option>
<option value="1" data-select2-id="6">Mr</option>
<option value="2" data-select2-id="7">Mrs</option>
<option value="3" data-select2-id="8">Miss</option>
<option value="4" data-select2-id="9">Ms</option>
<option value="5" data-select2-id="10">Dr</option>
<option value="6" data-select2-id="11">Prof</option>
<option value="7" data-select2-id="12">Hon</option>
<option value="8" data-select2-id="13">Sir</option>
<option value="9" data-select2-id="14">Lord</option>
</select>
<span class="select2 select2-container select2-container--default select2-container--below" dir="ltr" data-select2-id="1" style="width: 100%;">
<span class="selection">
<span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="-1" aria-disabled="false" aria-labelledby="select2-ApplicantTitle-container">
<span class="select2-selection__rendered" id="select2-ApplicantTitle-container" role="textbox" aria-readonly="true"> </span>
<span class="select2-selection__arrow" role="presentation">
<b role="presentation"></b>
</span>
</span>
</span>
<span class="dropdown-wrapper" aria-hidden="true"></span>
</span>
<span class="select2 select2-container select2-container--default" dir="ltr" data-select2-id="1" style="width: 100%;">
<span class="selection">
<span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-disabled="false" aria-labelledby="select2-ApplicantTitle-container">
<span class="select2-selection__rendered" id="select2-ApplicantTitle-container" role="textbox" aria-readonly="true"> </span>
<span class="select2-selection__arrow" role="presentation">
<b role="presentation"></b>
</span>
</span>
</span>
<span class="dropdown-wrapper" aria-hidden="true"></span>
</span>
选择元素被隐藏,我不能简单地这样做
.....find_elements_by_xpath('./following::select//option[text()="Mr"]')[0].click()
如果您将选择Mr
然后内部span
元素
将更改为选定的选项
有人知道如何与中间select
元素交互吗?
或任何建议将不胜感激
编辑1:
我可以用这个 id 按下元素,select2-ApplicantTitle-container
然后下拉菜单会出现,但是从这个打开的列表中选择的方法是什么?
解决方案
试试下面的代码::
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@name='ApplicantTitle']")))
driver.execute_script("arguments[0].scrollIntoView();", element)
select = Select(element)
select.select_by_visible_text('Mr')
注意:将以下内容添加到您的解决方案中
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
您可以通过多种方式处理下拉菜单:
- select_by_index(索引)
- select_by_value(值)
- select_by_visible_text(文本)
** 解决方案 2:**
driver.get('https://www.santanderforintermediaries.co.uk/calculators-and-forms/affordability/')
wait = WebDriverWait(driver, 10)
element= wait.until(EC.element_to_be_clickable((By.XPATH, "//span[@id='ddlMethodRepaymentSelectBoxIt']")))
element.click()
element1= wait.until(EC.element_to_be_clickable((By.XPATH, "//ul[@id='ddlMethodRepaymentSelectBoxItOptions']//li[4]/a")))
element1.click()
解决方案3:
driver.get("https://www.santanderforintermediaries.co.uk/calculators-and-forms/affordability/")
driver.implicitly_wait(10)
element1 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "ddlMethodRepaymentSelectBoxIt")))
element1.click()
elements = WebDriverWait(driver, 30).until(
EC.visibility_of_all_elements_located((By.XPATH, "//ul[@id='ddlMethodRepaymentSelectBoxItOptions']//li[*]")))
expected_value = "Capital and interest"
for element in elements:
if element.text == expected_value:
element.click()
break
推荐阅读
- python - 如何修改数据集以进行训练
- solidity - 工厂错误调用的函数应该是应付的
- javascript - 函数开始使用状态之前的 Spinner(CircularProgress)
- r - 如何在 R 中使用 tryCatch 将值设置为 0?
- sql - SQL where子句在子表中查找包含混合值的记录
- python - 如何在python和beautifulsoup中获取表格内的百分比值
- haskell - IHP:如何使用来自其他字段的值验证字段
- python - 如何通过python做3维数组的累积和?(用于 3 维循环)
- python - 我怎样才能用 np.cumsum 写出这个滚动平均值
- odoo-14 - 在 odoo 14 销售点购买