[英]Is it possible to overwrite the value of <Input type = "hidden"> by letting user select from a drop down list?
[英]How to select or pass value into hidden type for drop down picklist on website?
[selenium和HTML上的新功能]
我想從網站上選擇一個下拉菜單。 該type
是隱藏的。 我只想從下拉列表中傳遞或選擇male
或female
,或將其傳遞給value
變量,我該怎么做?
我使用了chrome中的inspect元素來確定以下兩行是選擇值所需的行。
<div class="Select has-value is-clearable is-searchable Select--single">
<input name="customer.gender" type="hidden" value="female">
我從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')
上面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>
先感謝您。
編輯:
我從下拉列表中單擊的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:
下拉菜單中的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>
您的下拉列表是一個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();
節點內的下拉選項: <div class="Select-menu-outer">
,單擊該下拉列表后,此節點將附加到頁面,選擇一個選項后,將從頁面取消附加。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.