我在Python中使用Selenium Kit,并尝试从下拉菜单中选择一个选项。
为此,我正在使用python driver.select_by_visible_text()
。我的问题是,可见文本始终包含我要查找的值,但之后又添加了一些内容。在select_by_visible_text()
刚刚找到确切的选项,但我不能确切的名字。
例如:我正在寻找选项“ W33”,然后网站显示“ W33(仅剩4个)”。我想选择“ W33(仅剩4个)”,但是不知道如何实现?
您可以options
在Select
对象上获取具有属性的所有选项的列表:
from selenium.webdriver.support.ui import Select
elem = driver.find_element_by_id('myselect')
elem_select = Select(elem)
opts = elem_select.options
然后,检查其中哪一个匹配。在您的示例中,检查text
属性:
opts_to_select = [o for o in opts if o.text.startswith('W33')]
my_option = opts_to_select[0] # select first match
# (Maybe you also want to raise an error if
# there is more than one match.)
并选择它:
if not my_elem.is_selected():
my_elem.click()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句