簡體   English   中英

列出Selenium Python中的選定選項

[英]List selected options in Selenium Python

我正在嘗試列出頁面中的選定選項。 但是下面的代碼沒有任何內容。 任何幫助將不勝感激。

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">
    <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>

我試過的

  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)

我嘗試了許多其他選擇,但未能成功。

只需注釋掉'if'語句並打印(option.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)

PS您的if語句引用的是未定義的“標簽”對象。 所以它永遠不會匹配任何東西

使用Select處理<select>元素:

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"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM