[英]How to select item from drop down menu using Selenium with Python?
I have a drop down menu and need to select an item based on its value.我有一个下拉菜单,需要 select 一个项目基于它的价值。 let's say I want to select value "50.0"假设我想 select 值“50.0”
full HTML of the table is at the end of the post: below code can work based on the ID (but the problem is that everytime I reload the page the ID changes so I have to update it in my code).表格的完整 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()
what should be the code to use to select the value 50.0 from the dropdown box based on its value and not the ID?应该是什么代码用于 select 基于其值而不是 ID 从下拉框中的值 50.0?
Below is the HTML of this dropdown menu:以下是此下拉菜单的 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 +
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
for example: let's assume the dropdown has an id "12345"例如:假设下拉列表的 id 为“12345”
and there is:还有:
< option value="x"> A < /option>
< option value="y"> B < /option>
< option value="z"> C < /option>
first capture the options in selenium: => options_list = new Select(driver.findElement(By.id("1234")));首先捕获 selenium 中的选项: => options_list = new Select(driver.findElement(By.id("1234")));
if you want to select by visible dropdown item: (lets say "A" ) => options_list.selectByVisibleText("A")如果你想通过可见的下拉项 select :(让我们说“A”)=> options_list.selectByVisibleText(“A”)
if you want to select the item by dropdown number (lets say second item) => options_list.selectByIndex(1)如果你想 select 下拉数字的项目(比如说第二个项目)=> options_list.selectByIndex(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.