[英]With Python Selenium, how to select an item from a single select drop-down menu, when there are multiple?
[英]How to select item from drop down menu using Selenium with Python?
我有一个下拉菜单,需要 select 一个项目基于它的价值。 假设我想 select 值“50.0”
表格的完整 HTML 在帖子的末尾:下面的代码可以根据 ID 工作(但问题是每次我重新加载页面时 ID 都会更改,所以我必须在我的代码中更新它)。
width = browser.find_element_by_css_selector("#linemodechannelwidth > tbody > tr > td.dijitReset.dijitStretch.dijitButtonContents > div.dijitReset.dijitInputField.dijitButtonText > span")
actions3 = ActionChains(browser)
actions3.click(width).perform()
time.sleep(4)
spacing50 = browser.find_element_by_css_selector('#dijit_MenuItem_27_text')
spacing50.click()
应该是什么代码用于 select 基于其值而不是 ID 从下拉框中的值 50.0?
以下是此下拉菜单的 HTML:
<table class="dijit dijitReset dijitMenuTable dijitSelectMenu dijitValidationTextBoxMenu dijitMenu dijitMenuPassive" role="listbox" tabindex="0" cellspacing="0" id="linemodechannelwidth_menu" widgetid="linemodechannelwidth_menu" style="top: 0px; visibility: visible;" aria-labelledby="linemodechannelwidth">
<tbody class="dijitReset" data-dojo-attach-point="containerNode"><tr class="dijitReset dijitMenuItem" data-dojo-attach-point="focusNode" role="option" tabindex="-1" id="dijit_MenuItem_27" aria-label="50.0 " aria-disabled="false" widgetid="dijit_MenuItem_27" aria-selected="false" style="user-select: none;">
<td class="dijitReset dijitMenuItemIconCell" role="presentation">
<span role="presentation" class="dijitInline dijitIcon dijitMenuItemIcon dijitNoIcon" data-dojo-attach-point="iconNode"></span>
</td>
<td class="dijitReset dijitMenuItemLabel" colspan="2" data-dojo-attach-point="containerNode,textDirNode" role="presentation" id="dijit_MenuItem_27_text">50.0</td>
<td class="dijitReset dijitMenuItemAccelKey" style="display: none" data-dojo-attach-point="accelKeyNode" id="dijit_MenuItem_27_accel"></td>
<td class="dijitReset dijitMenuArrowCell" role="presentation">
<span data-dojo-attach-point="arrowWrapper" style="visibility: hidden">
<span class="dijitInline dijitIcon dijitMenuExpand"></span>
<span class="dijitMenuExpandA11y">+</span>
</span>
</td>
37.5 + 62.5 + 75.0 + 87.5 +
from selenium.webdriver.remote.webelement import WebElement
### Find the table first
table = browser.find_element_by_css_selector(
'table.dijit.dijitReset.dijitMenuTable.dijitSelectMenu.dijitValidationTextBoxMenu.dijitMenu.dijitMenuPassive'
)
### Loop through all td in the table
for td in table.find_elements_by_tag_name('td'):
assert isinstance(td, WebElement)
### If the text value cast to int is 50 click and break.
if int(td.text) == 50:
td.click()
break
例如:假设下拉列表的 id 为“12345”
还有:
< option value="x"> A < /option>
< option value="y"> B < /option>
< option value="z"> C < /option>
首先捕获 selenium 中的选项: => options_list = new Select(driver.findElement(By.id("1234")));
如果你想通过可见的下拉项 select :(让我们说“A”)=> options_list.selectByVisibleText(“A”)
如果你想 select 下拉数字的项目(比如说第二个项目)=> options_list.selectByIndex(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.