简体   繁体   English

Selenium Wrapper 从下拉列表中随机选择一个元素(提供下拉列表的位置(xpath,css,...))?

[英]Selenium Wrapper to randomly select an element from a dropDown ( the location (xpath, css, ...) of the dropdown is provided)?

我想制作硒包装器以随机选择测试参数,例如可以从下拉列表中选择随机值......

Create list for all possible values from drop down for example you have drop down for country so your list will look like从下拉列表中为所有可能的值创建列表,例如您有国家/地区的下拉列表,因此您的列表看起来像

country = ['Australia', 'INDIA', 'CANADA', 'etc']

Make list using Visible text from DOM.使用来自 DOM 的可见文本制作列表。 Use choice function to select randomly使用选择功能随机选择

.select_by_visible_text(random.choice(country ))

Another option is using index另一种选择是使用索引

country = Select(driver.find_elements(By.XPATH, '//xpath'))
country .select_by_index(randint(0, len(country .options) - 1))

Hope this helps希望这可以帮助

如何从不在下拉列表中的 select 值<div>使用 selenium python</div><div id="text_translate"><p> 我有下面的 html 代码,我想点击下拉列表和 select 第一个值。如何实现这一点。我在从下拉列表中选择值时遇到问题,但能够单击下拉列表</p><pre>&lt;div id= "location-select-list" class="mb-list" role="role0"&gt; &lt;mb-option id='1' class='classname' role='rolename' data-auto-id="dt1" aria-disabled="False" 1 &lt;/mb-option&gt; &lt;mb-option id='2' class='classname' role='rolename' data-auto-id="dt2"aria-disabled="False" 2 &lt;/mb-option&gt;</pre><p> 我已经尝试过了,但没有工作。</p><pre> #click on the dropdown--working WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, xpath_0))).click() #selecting 1st value from the dropdown value list--not working xpath = "//div[@id='location-select-list']//mb-option[@data-auto-id='dt1']" WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, xpath))).click()</pre></div> - How to select value from dropdown which is not in <div> using selenium python

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Selenium webdriver 获取 XPATH 和 select 下拉列表 - Python Selenium webdriver get XPATH and select dropdown 如何使用 Selenium 从下拉菜单中获取 select 个元素? - How to select element(s) from dropdown menu using Selenium? 从下拉列表中选择Python selenium选项 - Python selenium select option from dropdown UnexpectedTagNameException:消息:选择仅适用于<select>元素,不在<li>使用 Selenium 从下拉列表中选择 li 元素时出错 - UnexpectedTagNameException: Message: Select only works on <select> elements, not on <li>error selecting li element from a Dropdown using Selenium 从“选择”中选择没有 ID、名称或 XPATH 的下拉项? - Select Dropdown Item from "Select" with No ID, Name, or XPATH? 如何使用Python Selenium从Bootstrap下拉列表中选择元素? - How do I select an element from a Bootstrap dropdown using Python Selenium? 使用Python + Selenium选择一个下拉菜单 - Select a dropdown using Python + Selenium 无法单击硒中的下拉元素 - unable to click the dropdown element in selenium 如何从不在下拉列表中的 select 值<div>使用 selenium python</div><div id="text_translate"><p> 我有下面的 html 代码,我想点击下拉列表和 select 第一个值。如何实现这一点。我在从下拉列表中选择值时遇到问题,但能够单击下拉列表</p><pre>&lt;div id= "location-select-list" class="mb-list" role="role0"&gt; &lt;mb-option id='1' class='classname' role='rolename' data-auto-id="dt1" aria-disabled="False" 1 &lt;/mb-option&gt; &lt;mb-option id='2' class='classname' role='rolename' data-auto-id="dt2"aria-disabled="False" 2 &lt;/mb-option&gt;</pre><p> 我已经尝试过了,但没有工作。</p><pre> #click on the dropdown--working WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, xpath_0))).click() #selecting 1st value from the dropdown value list--not working xpath = "//div[@id='location-select-list']//mb-option[@data-auto-id='dt1']" WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, xpath))).click()</pre></div> - How to select value from dropdown which is not in <div> using selenium python 如何使用 Selenium 和 Python 从下拉列表中获取 select 的值 - How to select a value from DropDown list using Selenium and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM