从Selenium Python中的下拉列表中选择选项值
我有以下HTML元素:
<div class="inlineCenter GuestNumDropdown">
<select name="participants" id="ui-id-1" style="display: none;">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><span tabindex="0" id="ui-id-1-button" role="combobox" aria-expanded="true" aria-autocomplete="list" aria-owns="ui-id-1-menu" aria-haspopup="true" class="ui-selectmenu-button inputElement required ui-button ui-widget ui-selectmenu-button-open ui-corner-top"
aria-activedescendant="ui-id-24" aria-labelledby="ui-id-21" aria-disabled="false"><span class="ui-selectmenu-icon ui-icon ui-icon-triangle-1-s"></span><span class="ui-selectmenu-text">1</span></span>
<div class="ui-selectmenu-menu ui-front ui-selectmenu-open"
style="top: 980px; left: 88.5px;">
<ul aria-hidden="false" aria-labelledby="ui-id-1-button" id="ui-id-1-menu" role="listbox" tabindex="0" class="ui-menu ui-corner-bottom ui-widget ui-widget-content overflow" aria-activedescendant="ui-id-24" aria-disabled="false" style="width: 60px;">
<li class="ui-menu-item">
<div id="ui-id-21" tabindex="-1" role="option" class="ui-menu-item-wrapper">1</div>
</li>
<li class="ui-menu-item">
<div id="ui-id-22" tabindex="-1" role="option" class="ui-menu-item-wrapper">2</div>
</li>
<li class="ui-menu-item">
<div id="ui-id-23" tabindex="-1" role="option" class="ui-menu-item-wrapper">3</div>
</li>
<li class="ui-menu-item">
<div id="ui-id-24" tabindex="-1" role="option" class="ui-menu-item-wrapper ui-state-active">4</div>
</li>
</ul>
</div>
</div>
我在从下拉列表中选择“ 4”选项时遇到困难。
我尝试了以下代码,
(1)
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@class="ui-menu-item-wrapper ui-state-active" and text()="4"]'))))
(2)
pax = driver.find_element_by_id("ui-id-1")
pax.send_keys("4")
(3),
select= Select(driver.find_element_by_id("ui-id-1"))
select.select_by_value('4')
但它们似乎都没有用。请帮忙。
PS每次选项的XPATH中的ID都会改变
I have this following HTML element:
<div class="inlineCenter GuestNumDropdown">
<select name="participants" id="ui-id-1" style="display: none;">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><span tabindex="0" id="ui-id-1-button" role="combobox" aria-expanded="true" aria-autocomplete="list" aria-owns="ui-id-1-menu" aria-haspopup="true" class="ui-selectmenu-button inputElement required ui-button ui-widget ui-selectmenu-button-open ui-corner-top"
aria-activedescendant="ui-id-24" aria-labelledby="ui-id-21" aria-disabled="false"><span class="ui-selectmenu-icon ui-icon ui-icon-triangle-1-s"></span><span class="ui-selectmenu-text">1</span></span>
<div class="ui-selectmenu-menu ui-front ui-selectmenu-open"
style="top: 980px; left: 88.5px;">
<ul aria-hidden="false" aria-labelledby="ui-id-1-button" id="ui-id-1-menu" role="listbox" tabindex="0" class="ui-menu ui-corner-bottom ui-widget ui-widget-content overflow" aria-activedescendant="ui-id-24" aria-disabled="false" style="width: 60px;">
<li class="ui-menu-item">
<div id="ui-id-21" tabindex="-1" role="option" class="ui-menu-item-wrapper">1</div>
</li>
<li class="ui-menu-item">
<div id="ui-id-22" tabindex="-1" role="option" class="ui-menu-item-wrapper">2</div>
</li>
<li class="ui-menu-item">
<div id="ui-id-23" tabindex="-1" role="option" class="ui-menu-item-wrapper">3</div>
</li>
<li class="ui-menu-item">
<div id="ui-id-24" tabindex="-1" role="option" class="ui-menu-item-wrapper ui-state-active">4</div>
</li>
</ul>
</div>
</div>
I'm having trouble selecting the option with the value '4' from the dropdown list.
I have tried the following codes,
(1)
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@class="ui-menu-item-wrapper ui-state-active" and text()="4"]'))))
(2)
pax = driver.find_element_by_id("ui-id-1")
pax.send_keys("4")
(3)
select= Select(driver.find_element_by_id("ui-id-1"))
select.select_by_value('4')
But none of them seems to work. Please help.
P.S. The id in the XPaths of the options changes every time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论