gpt4 book ai didi

python - Selenium -Python : unable to click an option in a select

转载 作者:行者123 更新时间:2023-12-01 06:21:46 24 4
gpt4 key购买 nike

我正在尝试在选择元素中选择一个选项。该下拉列表还包含一个具有自动完成功能的搜索框。它看起来像这样:

enter image description here

我尝试了多种方法,但没有成功。

试验 1:select_by_visible_text

from selenium.webdriver.support.ui import Select
type_act = Select(driver.find_element_by_xpath("//*[@id=\"edit-field-activity-type-und\"]"))
type_act.select_by_visible_text("Begeleide uitstap - Excursie")

试验 2:select_by_index

type_act = Select(driver.find_element_by_name("field_activity_type[und]"))
type_act.select_by_index(1)

试验 3:循环单击选项

type_act = driver.find_element_by_xpath("//*[@id=\"edit-field-activity-type-und\"]")
for option in type_act.find_elements_by_tag_name("option"):
print(option.text)
if option.text == "Begeleide uitstap - Excursie":
option.click()
break

select 元素的 HTML 代码

<div class="field-type-taxonomy-term-reference field-name-field-activity-type field-widget-options-select form-wrapper" id="edit-field-activity-type">
<div class="form-item form-type-select form-item-field-activity-type-und">
<label for="edit-field-activity-type-und">Activiteitstype <span class="form-required" title="Dit veld is verplicht.">*</span></label>
<select class="chosen-enable form-select required chosen-processed" id="edit-field-activity-type-und" name="field_activity_type[und]" style="position: absolute; opacity: 0;">
<option value="_none">- Kies uit deze lijst -</option>
<option value="393">Begeleide uitstap - Excursie</option>
<option value="427">Beheerwerken</option>
<option value="425">Buiten - Met kinderen</option>
<option value="231">Concert</option>
<option value="389">Cursus en workshop</option>
<option value="399">Feest en eet &amp; drinkfestijn</option>
<option value="561">Fietstocht en wandelroute</option>
<option value="387">Film</option>
<option value="565">Inventarisatie</option>
<option value="391">Kamp en vakantie</option>
<option value="415">Knutselen - Met kinderen</option>
<option value="385">Lezing en congres</option>
<option value="407">Markt en braderie</option>
<option value="409">Monument &amp; erfgoed</option>
<option value="417">Museum - Met kinderen</option>
<option value="411">Natuur en landschap</option>
<option value="563">Observatie</option>
<option value="503">Onbepaald</option>
<option value="431">Onderzoek/Studie</option>
<option value="413">Opendeurdag</option>
<option value="397">Quiz en spel</option>
<option value="405">Tentoonstelling</option>
<option value="435">Vergadering</option>
<option value="559">Wandeling</option>
</select>
<div class="chosen-container chosen-container-single" style="width: 200px;" title="" id="edit_field_activity_type_und_chosen">
<a class="chosen-single" tabindex="-1">
<span>Begeleide uitstap - Excursie</span>
<div>
<b></b>
</div>
</a>
<div class="chosen-drop">
<div class="chosen-search">
<input type="text" autocomplete="off">
</div>
<ul class="chosen-results">
<li class="active-result result-selected" style="" data-option-array-index="0">- Kies uit deze lijst -</li>
<li class="active-result result-selected" style="" data-option-array-index="1">Begeleide uitstap - Excursie</li>
<li class="active-result" style="" data-option-array-index="2">Beheerwerken</li>
<li class="active-result" style="" data-option-array-index="3">Buiten - Met kinderen</li>
<li class="active-result" style="" data-option-array-index="4">Concert</li>
<li class="active-result" style="" data-option-array-index="5">Cursus en workshop</li>
<li class="active-result" style="" data-option-array-index="6">Feest en eet &amp; drinkfestijn</li>
<li class="active-result" style="" data-option-array-index="7">Fietstocht en wandelroute</li>
<li class="active-result" style="" data-option-array-index="8">Film</li>
<li class="active-result" style="" data-option-array-index="9">Inventarisatie</li>
<li class="active-result" style="" data-option-array-index="10">Kamp en vakantie</li>
<li class="active-result" style="" data-option-array-index="11">Knutselen - Met kinderen</li>
<li class="active-result" style="" data-option-array-index="12">Lezing en congres</li>
<li class="active-result" style="" data-option-array-index="13">Markt en braderie</li>
<li class="active-result" style="" data-option-array-index="14">Monument &amp; erfgoed</li>
<li class="active-result" style="" data-option-array-index="15">Museum - Met kinderen</li>
<li class="active-result" style="" data-option-array-index="16">Natuur en landschap</li>
<li class="active-result" style="" data-option-array-index="17">Observatie</li>
<li class="active-result" style="" data-option-array-index="18">Onbepaald</li>
<li class="active-result" style="" data-option-array-index="19">Onderzoek/Studie</li>
<li class="active-result" style="" data-option-array-index="20">Opendeurdag</li>
<li class="active-result" style="" data-option-array-index="21">Quiz en spel</li>
<li class="active-result" style="" data-option-array-index="22">Tentoonstelling</li>
<li class="active-result" style="" data-option-array-index="23">Vergadering</li>
<li class="active-result" style="" data-option-array-index="24">Wandeling</li>
</ul>
</div>
</div>
</div>

我正在使用 chromedriver。没有错误,但没有选择任何内容。我也可以打印出选项,但单击方法似乎不起作用。有人可以帮我解决这个问题吗?

最佳答案

也许可以在该项目上尝试 .sendkeys() 来输入您要查找的文本?

from selenium.webdriver.common.keys import Keys
type_act = driver.find_element_by_xpath("//*[@id=\"edit-field-activity-type-und\"]")
type_act.send_keys("Begeleide uitstap - Excursie")
type_act.send_keys(Keys.ENTER)

关于python - Selenium -Python : unable to click an option in a select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60324682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com