關於處理webdriver裡下拉選單的問題
阿新 • • 發佈:2019-01-25
#!/user/bin/env python # coding = utf-8 import unittest import time from selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.by import By from selenium.webdriver.support.select import Select from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait # ***類裡的方法是按字母順序執行的*** class SearchTest(unittest.TestCase): @classmethod def setUpClass(cls): # create a new Firefox session """ cls.driver = webdriver.Chrome() cls.driver.implicitly_wait(30) cls.driver.maximize_window() def test_homepage_login(self): self.driver.get("url") self.driver.find_element_by_id("login_account").send_keys("account") self.driver.find_element_by_id("login_password").send_keys("password") self.driver.find_element_by_id("loginBtn").click() time.sleep(3) self.assertTrue(self.is_element_present(By.ID, "exitBtn")) def test_homepage_menu(self): # 點選下拉選單 self.elem = self.driver.find_element_by_xpath("//*[@id='navUl']/li[2]") self.elem.click() # 找到nav-con-list父元素 WebDriverWait(self.driver, 10).until(lambda the_driver: the_driver.find_element_by_class_name("nav-con-list").is_displayed()) time.sleep(3) # 找到終端管理,為了便於觀察結果sleep 15s self.driver.find_element_by_class_name("nav-con-list").find_element_by_xpath("//*[@id = 'terminalMage']/a").click() time.sleep(15) @classmethod def tearDownClass(cls): # close the browser window cls.driver.quit() def is_element_present(self, how, what): """ Utility method to check presence of an element on page :params how: By locator type :params what: locator value """ try: self.driver.find_element(by=how, value=what) except NoSuchElementException: return False return True if __name__ == "__main__": unittest.main(verbosity=2)