2015-09-25 11 views
1

我有一個方法來檢查下拉字段是否有選定的值。 GUI中下拉列表中選擇的值是「PAF」。
我正在使用select類中的first_selected_option,它應該從下拉字段中檢查選定的值。 我使用If Then Else來檢查下拉列表中的值是否與「PAF」的期望值相匹配 我得到False而不是True。Selenium Python檢查是否選擇了具有預期值的下拉菜單。我得到假

這裏是我的硒webdriver的Python代碼片段:

def is_dropdown_selected_from_advanced_cleaning_output_tab(self, field): 
    selected_dropdown_option = Select(self.get_element(*MainPageLocators.mappings_paf_output_dropdown)).first_selected_option 
    print (str(selected_dropdown_option)) 
    if (str(selected_dropdown_option)) == field: 
     return True 
    else: 
     return False 
從我的TestCase類

的調用方法:

self.assertTrue(mappings.is_dropdown_selected_from_advanced_cleaning_output_tab("PAF")) 

在GUI中PAF在下拉菜單中選擇向下。爲什麼它返回false?我的代碼有什麼問題?

我想我沒有正確地獲取字符串值到變量selected_dropdown_option 我試過(str(selected_dropdown_option))這不是正確的方式來獲取字符串值嗎?

感謝, 里亞茲

回答

0

我haven`t測試這一點,但嘗試。它看起來像你試圖將你的webelement投射到一個字符串,你應該得到的文本。 e.g:selected_dropdown_option.text

def is_dropdown_selected_from_advanced_cleaning_output_tab(self, field):  
    selected_dropdown_option = Select(self.get_element(*MainPageLocators.mappings_paf_output_dropdown)) 
    selected_dropdown_option.select_by_visible_text(field) 
    selected_text = selected_dropdown_option.text 
    if(selected_text == field): 
     return True 
    else: 
     return False 
+0

我得到的錯誤:AttributeError的:選擇實例沒有屬性 '文本' –

+0

什麼first_selected_option辦?不應該告訴你在下拉字段中選擇了什麼嗎? –

+0

如果你這樣做: selected.text = selected_dropdown_option.first_selected_option.text 是的,應該給你選定的元素 – patricmj

相關問題