selenium自動化測試工具python筆試面試項目實戰(zhàn)5鍵盤操作

說明

本文參考答案基于Chrome压状,分辨率1920*1080舒憾,在其他環(huán)境表現(xiàn)可能會不同。
本文代碼地址

  • 參考書籍下載:

Learning Selenium Testing Tools with Python-2014.pdf

Selenium自動化測試 基于 Python 語言 - 2018.pdf

上機實操: 在新的TAB打開連接

  • 打開:https://china-testing.github.io/
  • 選擇"數(shù)據(jù)分析"欄目的文章
  • 按住"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.Chrome()
driver.get("https://china-testing.github.io/")
driver.implicitly_wait(30)
driver.maximize_window()
element = driver.find_element_by_link_text('數(shù)據(jù)分析')
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()                                                                                            
  • 面試問答
  1. driver.quit() 和 driver.close()有什么區(qū)別凤瘦?

2.selenium中按下和松開鍵如何表示溶浴?

3.簡述ActionChains類的作用?

上機實操: 基于坐標移動

圖片.png

參考答案

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 討論釘釘免費群21745728 qq群144081101 567351477
# CreateDate: 2018-10-20
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("http://guidebook.seleniumacademy.com/Selectable.html")
driver.implicitly_wait(30)
driver.maximize_window()
one= driver.find_element_by_name('one')
print(one.location)
print(one.size)
six = driver.find_element_by_name('six')
print(six.location)
print(six.size)
actions = ActionChains(driver)
actions.move_by_offset(one.size['width'] + one.location['x'],
                       one.size['height'] + one.location['y']).click().perform()
input('Press ENTER to close the automated browser')
driver.quit()                                                                                    
  • 面試問答
  1. 如果獲取元素的位置信息和大小闸迷?

2.move_by_offset()有什么作用瘩蚪?

上機實操: 發(fā)送delete鍵在google drive上刪除文件

  • 準備能訪問google drive,并在firefox上保存了登錄的賬號稿黍。
  • 打開:https://drive.google.com
  • 點擊第一個文件,進行刪除
  • 按上述步驟循環(huán)刪除所有文件
圖片.png

參考答案

# -*- coding: utf-8 -*-
# 討論釘釘免費群21745728 qq群144081101 567351477
# CreateDate: 2018-10-20
 
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

fp = webdriver.FirefoxProfile(r'C:\Users\acer\AppData\Roaming\Mozilla\Firefox\Profiles\ar8m1pr8.default')
driver = webdriver.Firefox(fp)
driver.get("https://drive.google.com")
driver.implicitly_wait(20)
driver.maximize_window()

elements = driver.find_elements_by_xpath("http://div[3]/span")
while elements:
    ActionChains(driver).click(elements[0]).send_keys(Keys.DELETE).perform()
    elements = driver.find_elements_by_xpath("http://div[3]/span")
    
input('Press ENTER to close the automated browser')
driver.quit()                                                                          
  • 面試問答
  1. 如果找到實際打開瀏覽器默認的firebox profile崩哩?

2.如何發(fā)送DELETE鍵巡球?

上機實操: 驗證懸浮提示內容

圖片.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.Chrome()
        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()有什么用途?

上機實操: 雙擊改變顏色

圖片.png
  • 雙擊上圖藍色的框,把顏色該變成黃色
  • 參考答案
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 討論釘釘免費群21745728 qq群144081101 567351477
# CreateDate: 2018-10-18

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import unittest


class DoubleClickTest (unittest.TestCase):

    URL = 'http://api.jquery.com/dblclick/'

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get(self.URL)
        self.driver.maximize_window()

    def test_double_click(self):
        driver = self.driver
        frame = driver.find_element_by_tag_name('iframe')
        driver.switch_to.frame(frame)
        box = driver.find_element_by_tag_name('div')

        # verify color is Blue
        self.assertEqual('rgba(0, 0, 255, 1)',
                         box.value_of_css_property('background-color'))

        ActionChains(driver).move_to_element(
            driver.find_element_by_tag_name('body')).perform()
        ActionChains(driver).double_click(box).perform()

        # verify Color is Yellow
        self.assertEqual('rgba(255, 255, 0, 1)',
                         box.value_of_css_property('background-color'))

    def tearDown(self):
        self.driver.close()

if __name__ == '__main__':
    unittest.main(verbosity=2)

  • 面試問答

1.double_click()有什么用途?

2.rgba的含義邓嘹?

上機實操: 在新的TAB打開連接

圖片.png
  • 拖動左邊的框到右邊

參考答案

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 討論釘釘免費群21745728 qq群144081101 567351477
# CreateDate: 2018-10-18
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import unittest


class DragAndDropTest (unittest.TestCase):

    URL = 'http://jqueryui.com/resources/demos/droppable/default.html'

    def setUp(self) :
        self.driver = webdriver.Chrome()
        self.driver.get(self.URL)
        self.driver.maximize_window()

    def test_drag_and_drop(self):
        driver = self.driver

        source = driver.find_element_by_id('draggable')
        target = driver.find_element_by_id('droppable')

        ActionChains(self.driver).drag_and_drop(source, target).perform()
        self.assertEqual('Dropped!', target.text)

    def tearDown(self):
        self.driver.close()

if __name__ == '__main__':
    unittest.main(verbosity=2)

  • 面試問答

1.drag_and_drop()有什么用途?

selenium 上機實操: 下拉刷新框中所有內容(實現(xiàn))

圖片.png

參考答案

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 討論釘釘免費群21745728 qq群144081101 567351477
# CreateDate: 2018-10-18

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 as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

class at_least_n_elements_found(object):
    def __init__(self, locator, n):
        self.locator = locator
        self.n = n
        
    def __call__(self, driver):
        elements = driver.find_elements(*self.locator)
        if len(elements) >= self.n:
            return elements
        else:
            return False
        
url = 'http://www.webscrapingfordatascience.com/complexjavascript/'
driver = webdriver.Chrome()
driver.get(url)
# Use an implicit wait for cases where we don't use an explicit one
driver.implicitly_wait(10)
div_element = driver.find_element_by_class_name('infinite-scroll')
quotes_locator = (By.CSS_SELECTOR, ".quote:not(.decode)")

nr_quotes = 0
while True:
    
    # Scroll down to the bottom, now using action (chains)
    action_chain = ActionChains(driver)
    # Move to our quotes block
    action_chain.move_to_element(div_element)
    # Click it to give it focus
    action_chain.click()
    # Press the page down key about 10 ten times
    action_chain.send_keys([Keys.PAGE_DOWN for i in range(10)])
    # Do these actions
    action_chain.perform()
    
    # Try to fetch at least nr_quotes+1 quotes
    try:
        all_quotes = WebDriverWait(driver, 3).until(
            at_least_n_elements_found(quotes_locator, nr_quotes + 1))
    except TimeoutException as ex:
        # No new quotes found within 3 seconds, assume this is all there is
        print("... done!")
        break
    
    # Otherwise, update the quote counter
    nr_quotes = len(all_quotes)
    print("... now seeing", nr_quotes, "quotes")
    
# all_quotes will contain all the quote elements
print(len(all_quotes), 'quotes found\n')
for quote in all_quotes:
    print(quote.text)
input('Press ENTER to close the automated browser')
driver.quit()
  • 面試問答

1.如何模擬鼠標中鍵下滾?

參考資料

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市棚贾,隨后出現(xiàn)的幾起案子窖维,更是在濱河造成了極大的恐慌,老刑警劉巖妙痹,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件铸史,死亡現(xiàn)場離奇詭異,居然都是意外死亡怯伊,警方通過查閱死者的電腦和手機琳轿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來耿芹,“玉大人崭篡,你說我怎么就攤上這事“娠酰” “怎么了琉闪?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長砸彬。 經(jīng)常有香客問我塘偎,道長,這世上最難降的妖魔是什么拿霉? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任吟秩,我火速辦了婚禮,結果婚禮上绽淘,老公的妹妹穿的比我還像新娘涵防。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布壮池。 她就那樣靜靜地躺著偏瓤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪椰憋。 梳的紋絲不亂的頭發(fā)上厅克,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天,我揣著相機與錄音橙依,去河邊找鬼证舟。 笑死,一個胖子當著我的面吹牛窗骑,可吹牛的內容都是我干的女责。 我是一名探鬼主播,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼创译,長吁一口氣:“原來是場噩夢啊……” “哼抵知!你這毒婦竟也來了?” 一聲冷哼從身側響起软族,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤刷喜,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后立砸,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吱肌,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年仰禽,在試婚紗的時候發(fā)現(xiàn)自己被綠了氮墨。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡吐葵,死狀恐怖规揪,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情温峭,我是刑警寧澤猛铅,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站凤藏,受9級特大地震影響奸忽,放射性物質發(fā)生泄漏。R本人自食惡果不足惜揖庄,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一栗菜、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蹄梢,春花似錦疙筹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽霍比。三九已至,卻和暖如春暴备,著一層夾襖步出監(jiān)牢的瞬間悠瞬,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工涯捻, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留浅妆,地道東北人。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓汰瘫,卻偏偏與公主長得像,于是被迫代替她去往敵國和親擂煞。 傳聞我的和親對象是個殘疾皇子混弥,可洞房花燭夜當晚...
    茶點故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內容