在Selenium Python中列出所选选项
我正在尝试从页面中列出所选选项。但是,没有打印以下代码。任何帮助都将不胜感激 HTML代码:在Selenium Python中列出所选选项,python,python-3.x,selenium,web-scraping,Python,Python 3.x,Selenium,Web Scraping,我正在尝试从页面中列出所选选项。但是,没有打印以下代码。任何帮助都将不胜感激 HTML代码: <select class="chosen-select" id="tag_opts" name="tag_opts[]" tabindex="-1" multiple="true" data-placeholder="Select a call tag here" disabled="disabled"> <optgroup label="TAGS"> <opti
<select class="chosen-select" id="tag_opts" name="tag_opts[]" tabindex="-1" multiple="true" data-placeholder="Select a call tag here" disabled="disabled">
<optgroup label="TAGS">
<option value="1" selected="selected">1</option>
<option value="2" selected="selected">2</option>
<option value="3" selected="selected">3</option>
<option value="4" selected="selected">4</option>
</optgroup>
</select>
我尝试了许多其他选项,但没有成功。只需注释掉“if”语句并打印(option.text)
PS if语句引用了未定义的“labels”对象。因此它永远不会匹配任何内容使用
选择来处理
元素:
select = Select(driver.find_element_by_id("tag_opts"))
options = select.options
for option in options:
print(option.text) #or print(option.get_attribute("value"))
是否在刮取html代码时未加载?上述代码包含在框架中。当我列出所有page\u source
code时,会显示以上选项。看来html代码已经加载了。你需要先切换到框架
。是的,我已经这样做了。请发布你所有的代码<代码>标签
未定义。我将尝试一下。通过上述更改,控制台中只打印了4行空白行。Shivam使用打印(选项。获取属性(“值”)
,成功了。您可以编辑答案。谢谢。@experiator,尽管它对您不起作用,.text
也是一个解决方案,所以我会保持我的答案不变(两种解决方案都是):)好的。谢谢你的确认。
el = driver.find_element_by_id("tag_opts")
for option in el.find_elements_by_tag_name('option'):
#if option.text in labels:
print(option.text)
select = Select(driver.find_element_by_id("tag_opts"))
options = select.options
for option in options:
print(option.text) #or print(option.get_attribute("value"))