简体   繁体   English

在 Selenium PYTHON 中使用 span id 从下拉列表中选择一个值

[英]Selecting a value from drop down with span id in Selenium PYTHON

I have been trying this for a while and searched different forums but I couldn't find any snippet to do this.我已经尝试了一段时间并搜索了不同的论坛,但我找不到任何代码片段来执行此操作。

I have a report in which options need to be selected through dropdown using Selenium in python.我有一份报告,其中需要在 python 中使用 Selenium 通过下拉列表选择选项。 Below is the HTML structure下面是 HTML 结构

 <div align="center"> Select Fruit</div> <p align="center"><br> <span id="0e6b87875e914a5f8d72bbee6844bea3" style="color: black; font-family: Arial; font-size: 13px; font-weight: normal; font-style: normal; width: 113px; display: inline-block;" class="sf-element sf-element-control sfc-property sfc-dropdown"> <div class="sf-element sf-element-dropdown" title="" style="position: relative; width: 100px;"> <div class="sf-element sf-element-icon" style="position: absolute; top: 1px; left: 91px; height: 17px; width: 17px;"> <svg width="17px" height="17px"><path d="M4,6 l7,0 l-3.5,3.5 z" fill="currentColor" stroke-width="1" transform="scale(1.1333333333333333,1.1333333333333333)" class="Down"></path></svg> </div> <div class="sf-element sf-element-text-box" style="display: inline-block; word-wrap: break-word; width: 83px;">(None)</div> <select class="sf-dropdown-select" style="color: rgb(0, 0, 0); font-family: Arial; background-color: rgb(248, 248, 248);"> <option value="0" selected="selected">(None)</option> <option value="1">Apple</option> <option value="2">Mango</option> <option value="3">Grapes</option> </select> </div> </span><br></p>

I have tried different ways using css selector and XPath but nothing seems to work.我尝试过使用 css 选择器和 XPath 的不同方法,但似乎没有任何效果。 Below is the code I tried下面是我试过的代码

driver.find_element_by_xpath('//*[@id="0e6b87875e914a5f8d72bbee6844bea3"]/div/select/option[@value = "Mango"]')

Also different variants like options[2] and using css selector but it always give NoSuchElementException.还有不同的变体,如选项 [2] 和使用 css 选择器,但它总是给出 NoSuchElementException。

Can someone please share some insights on this?有人可以分享一些对此的见解吗?

Thanks谢谢

添加 text()="Mongo" 而不是 @value="Mongo"

 driver.find_element_by_xpath('//[@id="0e6b87875e914a5f8d72bbee6844bea3"]/div/select/option[text() = "Mango"]').click()

When dealing with SELECT elements, there is a helper class, Select , that makes it a lot easier.在处理 SELECT 元素时,有一个辅助类Select ,这使它变得更加容易。

select = Select(driver.find_element_by_css_selector("#0e6b87875e914a5f8d72bbee6844bea3 select"))
select.select_by_visible_text("Mango")

The dropdown is clearly within <Select> tag. dropdown显然在<Select>标签内。 Hence it would be convinient to use the Select Class as follows:因此,使用Select类会很方便,如下所示:

//import
from selenium.webdriver.support.ui import Select
//code block
selectOption = Select(driver.find_element_by_class_name("sf-dropdown-select"))
selectOption.select_by_visible_text("Mango")

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

相关问题 使用selenium python从下拉选项中选择一个值 - Selecting a value from a drop-down option using selenium python Selenium Python:从下拉列表中选择元素 - Selenium Python : pick element from drop down in a span 在python中使用硒从下拉菜单中选择多个选项 - Selecting multiple options from drop down menu using selenium in python 使用Selenium Webdriver从python的下拉列表中选择选项 - Selecting options from drop down list in python using Selenium Webdriver 从一个下拉项中选择一个<div>菜单硒蟒蛇 - selecting a drop down item from a <div> menu selenium python Python (Selenium) 从 HHPRED 中选择下拉列表 - Python (Selenium) Selecting a drop down list from HHPRED Span 标签中的 Python Selenium 下拉菜单 - Python Selenium drop-down in Span tag Python 3 Selenium Select 来自具有相同 ID 但值不同的下拉菜单 - Python 3 Selenium Select from drop down menu with the same ID but different value 使用 Selenium Python 选择下拉菜单 - 无法定位元素:{“method”:“css selector”,“selector”:"[id= - Selecting drop-down using Selenium Python - Unable to locate element: {“method”:“css selector”,“selector”:"[id= 无法使用python中的Selenium从下拉列表中选择一个值 - Cant select a value from a drop down list using Selenium in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM