首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python / Selenium如何与隐藏的选择元素交互

Python / Selenium如何与隐藏的选择元素交互
EN

Stack Overflow用户
提问于 2020-07-28 11:43:01
回答 3查看 1.8K关注 0票数 1

这是用于选择特定选项所需的元素的HTML代码

代码语言:javascript
运行
复制
<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">&nbsp;</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">&nbsp;</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">&nbsp;</span>
            <span class="select2-selection__arrow" role="presentation">
                <b role="presentation"></b>
            </span>
        </span>
    </span>
    <span class="dropdown-wrapper" aria-hidden="true"></span>
</span>

Select元素是隐藏的,我不能简单地执行这个.....find_elements_by_xpath('./following::select//option[text()="Mr"]')[0].click()

如果选择Mr,则span元素内部的&nbsp;将更改为所选选项

有人知道如何与隐藏的select元素交互吗?

否则任何建议都将不胜感激。

编辑1:

我可以用这个id select2-ApplicantTitle-container按下元素,然后会显示下拉列表,但是从这个打开的列表中选择什么方法呢?

EN

回答 3

Stack Overflow用户

发布于 2020-07-28 11:54:18

试试下面的代码:

代码语言:javascript
运行
复制
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')

注意:将下面的impoerts添加到解决方案中

代码语言:javascript
运行
复制
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

您可以通过多种方法处理掉这些问题:

  1. select_by_index(index)
  2. select_by_value(value)
  3. select_by_visible_text(text)

**解决方案2:**

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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
票数 0
EN

Stack Overflow用户

发布于 2020-07-28 12:17:50

您可以首先使用下面的CSS单击select列表,然后选择项。

代码语言:javascript
运行
复制
listEle = driver.find_element_by_css_selector("select#ApplicantTitle")
listEle.click()
#Then you can select the option 
listEle.find_element_by_xpath("./option[.='Mr']").click()
票数 0
EN

Stack Overflow用户

发布于 2020-07-28 12:24:50

UI似乎是一个下拉列表,但它不一样。如果HTML中有selected,而没有包装在其他标记中,那么可以使用Selenium类来选择它。

在参考站点中,您需要模拟相同的单击操作,以便从下拉列表中选择一个值。

使用以下代码:

代码语言:javascript
运行
复制
driver.get("https://www.santanderforintermediaries.co.uk/calculators-and-forms/affordability/")
driver.implicitly_wait(10)
# click on dropdown
driver.find_element_by_id("ddlMethodRepaymentSelectBoxIt").click()

#get all dropdown element
elements = driver.find_elements_by_css_selector("#ddlMethodRepaymentSelectBoxItOptions>li")

expected_value = "Part and part - sale of mortgaged property"

# loop through all all element and and check whether element has the expected value to select if yes the select and break out the loop
for element in elements:
    if element.text == expected_value:
        element.click()
        break
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63133340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档