简体   繁体   English

如何使用 Selenium 和 Python 从下拉菜单中的 select 项目?

[英]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.

相关问题 Python Selenium select 如何从单个 select 下拉菜单中的一个项目,当有多个? - With Python Selenium, how to select an item from a single select drop-down menu, when there are multiple? 如何使用 Python 与 Selenium 一起 select 下拉菜单值? - How to select a drop-down menu value with Selenium using Python? 如何选择一个下拉菜单值与硒使用 python - how-to-select-a-drop-down-menu-value-with-selenium-using-python 如何从 python selenium 的下拉菜单中获取 select 的值 - How to select a value from drop down menu in python selenium 如何使用 select 这个非 select 下拉菜单中的元素使用 Selenium Webdriver 和 ZA7F57F35426B9387411 - How to select this element from this non select drop-down menu using Selenium Webdriver and Python 如何使用硒从下拉菜单中随机选择? - How to randomly select from a drop-down menu using selenium? 使用 selenium Python 库单击下拉菜单中的项目 - Click an item in a drop-down menu using selenium Python library 从一个下拉项中选择一个<div>菜单硒蟒蛇 - selecting a drop down item from a <div> menu selenium python 如何使用 Selenium 从非选择下拉菜单中单击项目 - How to click an item from non select drop-down menu with Selenium 无法使用 python Selenium 选择下拉菜单 - Can not select drop down menu using python Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM