selenium自動化測試工具python筆試面試專案實戰5鍵盤操作
上機實操: 在新的TAB開啟連線
- 開啟: ofollow,noindex">https://china-testing.github.io/
- 選擇"資料分析"欄目的文章
- 按住"Ctrl+TAB"選擇"python"欄目的文章
- 切換到新的標籤"python"
- 關閉新的標籤"python"
- 關閉瀏覽器
參考答案
#!/usr/bin/python3 # -*- coding: utf-8 -*- # 討論釘釘免費群21745728 qq群144081101 567351477 # CreateDate: 2018-10-17 import time from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys driver = webdriver.Firefox() driver.get("https://china-testing.github.io/") driver.implicitly_wait(30) driver.maximize_window() element = driver.find_element_by_link_text('資料分析') element.click() time.sleep(3) element = driver.find_element_by_link_text('python') ActionChains(driver).key_down(Keys.CONTROL).click(element).key_up( Keys.CONTROL).perform() time.sleep(3) driver.switch_to.window(driver.window_handles[1]) time.sleep(3) driver.close() # 關閉當前TAB time.sleep(3) driver.quit()
- 面試問答
- driver.quit() 和 driver.close()有什麼區別?
2.selenium中按下和鬆開鍵如何表示?
3.簡述ActionChains類的作用?
上機實操: 驗證懸浮提示內容

圖片.png
- 滑鼠移動到上圖的"Your age:"
- 確認懸浮提示內容為'We ask for your age only for statistical purposes.'
- 關閉瀏覽器
- 參考答案
#!/usr/bin/python3 # -*- coding: utf-8 -*- # 討論釘釘免費群21745728 qq群144081101 567351477 # CreateDate: 2018-10-17 import unittest import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions from selenium.webdriver.common.action_chains import ActionChains class ToolTipTest (unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.get("http://jqueryui.com/tooltip/") self.driver.implicitly_wait(30) self.driver.maximize_window() def test_tool_tip(self): driver = self.driver frame_elm = driver.find_element_by_class_name('demo-frame') driver.switch_to.frame(frame_elm) time.sleep(3) age_field = driver.find_element_by_id('age') ActionChains(self.driver).move_to_element(age_field).perform() time.sleep(3) tool_tip_elm = WebDriverWait(self.driver, 10)\ .until(expected_conditions.visibility_of_element_located(( By.CLASS_NAME, 'ui-tooltip-content'))) # verify tooltip message self.assertEqual('We ask for your age only for statistical purposes.', tool_tip_elm.text) time.sleep(3) def tearDown(self): self.driver.close() if __name__ == '__main__': unittest.main(verbosity=2)
- 面試問答
1.move_to_element()有什麼用途?