文章以亚马逊北京控制台登录页面语言选择下拉框为例
完整代码
import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import Select
class LanguageOptions(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.driver.maximize_window()
self.driver.get('https://console.amazonaws.cn')
def test_language_option(self):
exp_options = ["", "English", "Deutsch", "Español", "Français", "日本語", "Português","한국어", "中文(简体)"]
act_options = []
select_language = Select(self.driver.find_element_by_id("languageSelector"))
self.assertEqual(9, len(select_language.options))
## 返回select元素所有的options
for option in select_language.options:
act_options.append(option.text)
self.assertListEqual(exp_options, act_options)
#first_selected_option:提供第一个被选中的选项元素
self.assertEqual("中文(简体)", select_language.first_selected_option.text)
select_language.select_by_visible_text('English')
select_language = Select(self.driver.find_element_by_id("languageSelector"))
select_language.select_by_index(0)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main(verbosity=2)