简体   繁体   English

如何在网站上的下拉列表中选择值或将值传递给隐藏类型?

[英]How to select or pass value into hidden type for drop down picklist on website?

[very new at selenium and HTML] [selenium和HTML上的新功能]

I want to select a drop down from a website. 我想从网站上选择一个下拉菜单。 The type is hidden. type是隐藏的。 I just want to pass or select either male or female from the drop down or pass it into the value variable, how would I do this? 我只想从下拉列表中传递或选择malefemale ,或将其传递给value变量,我该怎么做?

I used the inspect element in chrome to determine the two lines below are the ones required to select a value. 我使用了chrome中的inspect元素来确定以下两行是选择值所需的行。

<div class="Select has-value is-clearable is-searchable Select--single">
    <input name="customer.gender" type="hidden" value="female">

I got the xpath from chrome and tried to pass a value but did not work: 我从chrome获取了xpath并尝试传递一个值,但没有成功:

gender = driver.find_element_by_xpath("//*[@id='app']/div/div[1]/div[4]/div/div[2]/form/div[1]/div/div[2]/div[3]/div[2]/div/span[2]/div/input")
gender.send_keys('male')

The entire HTML of the above div element is: 上面div元素的整个HTML是:

<div class="Select has-value is-clearable is-searchable Select--single">
    <input name="customer.gender" type="hidden" value="female">
    <div class="Select-control">
        <span class="Select-multi-value-wrapper" id="react-select-5--value">
            <div class="Select-value">
                <span class="Select-value-label" role="option" aria-selected="true" id="react-select-5--value-item">Female</span>
            </div>
            <div class="Select-input" style="display: inline-block;">
                <input aria-activedescendant="react-select-5--value" aria-expanded="false" aria-haspopup="false" 
                    aria-owns="" role="combobox" value="" style="box-sizing: content-box; width: 5px;">
                <div style="position: absolute; top: 0px; left: 0px; visibility: hidden; height: 0px; overflow: scroll; white-space: pre; font-size: 14px; font-family: Helvetica, Arial, sans-serif; font-weight: 400; font-style: normal; letter-spacing: normal; text-transform: none;"></div>
            </div>
        </span>
        <span aria-label="Clear value" class="Select-clear-zone" title="Clear value">
            <span class="Select-clear">×</span>
        </span>
        <span class="Select-arrow-zone">
            <span class="Select-arrow"></span>
        </span>
    </div>
</div>

Thank you in advance. 先感谢您。

edit: 编辑:

HTML from where I click on the drop down without any values selected: 我从下拉列表中单击的HTML,但未选择任何值:

<div class="Select is-searchable Select--single">
    <div class="Select-control">
        <span class="Select-multi-value-wrapper" id="react-select-5--value">
            <div class="Select-placeholder">Select:</div>
            <div class="Select-input" style="display: inline-block;">
                <input aria-activedescendant="react-select-5--value" aria-expanded="false" aria-haspopup="false"
                    aria-owns="" role="combobox" value="" style="box-sizing: content-box; width: 5px;">
                <div style="position: absolute; top: 0px; left: 0px; visibility: hidden; height: 0px; 
                            overflow: scroll; white-space: pre; font-size: 14px; 
                            font-family: Helvetica, Arial, sans-serif; font-weight: 400; 
                            font-style: normal; letter-spacing: normal; text-transform: none;"></div>
            </div>
        </span>
        <span class="Select-arrow-zone"><span class="Select-arrow"></span></span>
    </div>
</div>

edit2: EDIT2:

HTML from value selected in drop down 下拉菜单中的HTML值

<div class="Select has-value is-clearable is-searchable Select--single">
    <input name="customer.gender" type="hidden" value="male">
    <div class="Select-control">
        <span class="Select-multi-value-wrapper" id="react-select-5--value">
            <div class="Select-value">
                <span class="Select-value-label" role="option" aria-selected="true" id="react-select-5--value-item">Male</span>
            </div>
            <div class="Select-input" style="display: inline-block;">
                <input aria-activedescendant="react-select-5--value" aria-expanded="false" aria-haspopup="false" 
                    aria-owns="" role="combobox" value="" style="box-sizing: content-box; width: 5px;">
                <div style="position: absolute; top: 0px; left: 0px; visibility: hidden; height: 0px; 
                            overflow: scroll; white-space: pre; font-size: 14px; 
                            font-family: Helvetica, Arial, sans-serif; font-weight: 400; 
                            font-style: normal; letter-spacing: normal; text-transform: none;"></div>
            </div>
        </span>
        <span aria-label="Clear value" class="Select-clear-zone" title="Clear value">
            <span class="Select-clear">×</span>
        </span>
        <span class="Select-arrow-zone"><span class="Select-arrow"></span></span>
    </div>
</div>

Your dropdown is a React Component, I get example from this site . 您的下拉列表是一个React组件,我从此站点获得了示例。

// click on dropdown to expand options
dirver.find_element_by_xpath("//span[@id='react-select-5--value']/..").click();

// choose option
dirver.find_element_by_xpath(
    "//span[@id='react-select-5--value']/../.." +
    "//div[@role='option'][text()='%s']".format("Male")).click();

The dropdown options inside node: <div class="Select-menu-outer"> , this node is attached to the page after you click on the dropdown, and de-attached from page after you choose one option. 节点内的下拉选项: <div class="Select-menu-outer"> ,单击该下拉列表后,此节点将附加到页面,选择一个选项后,将从页面取消附加。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以覆盖的值<Input type = "hidden">通过让用户从下拉列表中选择? - Is it possible to overwrite the value of <Input type = "hidden"> by letting user select from a drop down list? 如何从具有特殊设置的网站上使用Selenium从下拉列表中选择一个值-Python - How to select a value from a drop-down using Selenium from a website with special setting- Python 如何使用 Robot Framework 选择下拉菜单但输入元素类型 - How select a drop down but element type is input using Robot Framework 如何在 python 中使用 selenium 下拉列表的 select 值 - How to select value of drop down list with selenium in python 如何使用 Selenium、Python 选择下拉菜单值 - How to select a drop-down menu value with Selenium, Python 如何使用 Python 与 Selenium 一起 select 下拉菜单值? - How to select a drop-down menu value with Selenium using Python? 如何选择一个下拉菜单值与硒使用 python - how-to-select-a-drop-down-menu-value-with-selenium-using-python 如何从 Python 的下拉菜单中获取 select 的值 - How to select a value from drop down menu in Python 如何从 python selenium 的下拉菜单中获取 select 的值 - How to select a value from drop down menu in python selenium Python:如何从Selenium的下拉菜单中选择选项,元素已隐藏 - Python: How to select option from drop down menu in Selenium, element hidden
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM