Web自動化測試:switch_to包詳解:切換handle崖技、frame逻住、alert

之前章節(jié),分別對窗口切換(handle)迎献、frame切換瞎访、彈窗(alert)切換做了詳細的解釋,但是我們在寫代碼的時候發(fā)現(xiàn)吁恍,這些方法都被編輯器劃傷了一條橫線扒秸,但是方法還是可以正常使用,只是目前的pycharm不推薦你繼續(xù)這樣使用了(有新的方法可以替代它)冀瓦,那如果我們不使用這些方法的話伴奥,我們該怎么去完成切換窗口、frame這些操作呢翼闽?所以我們來學(xué)習(xí)一下替代這幾個方法的switch_to包拾徙。

一、switch_to包的方法詳解

在switch_to的基礎(chǔ)上肄程,有這么幾個方法锣吼,鑒于基本上都是之前曾經(jīng)講過的选浑,這次把等價的方法也列出來,供大家參考

driver.switch_to.parent_frame()

這是switch_to中獨有的方法玄叠,可以切換到上一層的frame古徒,對于層層嵌套的frame很有用

案例展示:

163郵箱登錄的例子來用新的switch_to方法寫一下,并通過觀察读恃,我們發(fā)現(xiàn)進入這個頁面后焦點直接就定位到輸入框里了隧膘,所以我們可以通過active_element()來定位。

from selenium import webdriver

from time import sleep

driver = webdriver.Chrome()

# 進入163郵箱首頁

driver.get("http://mail.163.com/")

sleep(2)

# 切換到包含登錄框的frame下

driver.switch_to.frame(1)

# 通過定位輸當(dāng)前焦點元素寺惫,并再次輸入數(shù)據(jù)

ele_box = driver.switch_to.active_element

ele_box.send_keys("123")

switch_to源碼

? ? class SwitchTo:

? ? def __init__(self, driver):

? ? ? ? self._driver = driver

? ? @property

? ? def active_element(self):

? ? ? ? """

? ? ? ? Returns the element with focus, or BODY if nothing has focus.

? ? ? ? :Usage:

? ? ? ? ? ? element = driver.switch_to.active_element

? ? ? ? """

? ? ? ? if self._driver.w3c:

? ? ? ? ? ? return self._driver.execute(Command.W3C_GET_ACTIVE_ELEMENT)

? ? ? ? else:

? ? ? ? ? ? return self._driver.execute(Command.GET_ACTIVE_ELEMENT)['value']

? ? @property

? ? def alert(self):

? ? ? ? """

? ? ? ? Switches focus to an alert on the page.

? ? ? ? :Usage:

? ? ? ? ? ? alert = driver.switch_to.alert

? ? ? ? """

? ? ? ? return Alert(self._driver)

? ? def default_content(self):

? ? ? ? """

? ? ? ? Switch focus to the default frame.

? ? ? ? :Usage:

? ? ? ? ? ? driver.switch_to.default_content()

? ? ? ? """

? ? ? ? self._driver.execute(Command.SWITCH_TO_FRAME, {'id': None})

? ? def frame(self, frame_reference):

? ? ? ? """

? ? ? ? Switches focus to the specified frame, by index, name, or webelement.

? ? ? ? :Args:

? ? ? ? - frame_reference: The name of the window to switch to, an integer representing the index,

? ? ? ? ? ? ? ? ? ? ? ? ? ? or a webelement that is an (i)frame to switch to.

? ? ? ? :Usage:

? ? ? ? ? ? driver.switch_to.frame('frame_name')

? ? ? ? ? ? driver.switch_to.frame(1)

? ? ? ? ? ? driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])

? ? ? ? """

? ? ? ? if isinstance(frame_reference, basestring) and self._driver.w3c:

? ? ? ? ? ? try:

? ? ? ? ? ? ? ? frame_reference = self._driver.find_element(By.ID, frame_reference)

? ? ? ? ? ? except NoSuchElementException:

? ? ? ? ? ? ? ? try:

? ? ? ? ? ? ? ? ? ? frame_reference = self._driver.find_element(By.NAME, frame_reference)

? ? ? ? ? ? ? ? except NoSuchElementException:

? ? ? ? ? ? ? ? ? ? raise NoSuchFrameException(frame_reference)

? ? ? ? self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})

? ? def parent_frame(self):

? ? ? ? """

? ? ? ? Switches focus to the parent context. If the current context is the top

? ? ? ? level browsing context, the context remains unchanged.

? ? ? ? :Usage:

? ? ? ? ? ? driver.switch_to.parent_frame()

? ? ? ? """

? ? ? ? self._driver.execute(Command.SWITCH_TO_PARENT_FRAME)

? ? def window(self, window_name):

? ? ? ? """

? ? ? ? Switches focus to the specified window.

? ? ? ? :Args:

? ? ? ? - window_name: The name or window handle of the window to switch to.

? ? ? ? :Usage:

? ? ? ? ? ? driver.switch_to.window('main')

? ? ? ? """

? ? ? ? data = {'name': window_name}

? ? ? ? if self._driver.w3c:

? ? ? ? ? ? data = {'handle': window_name}

? ? ? ? self._driver.execute(Command.SWITCH_TO_WINDOW, data)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末疹吃,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子西雀,更是在濱河造成了極大的恐慌萨驶,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件艇肴,死亡現(xiàn)場離奇詭異腔呜,居然都是意外死亡,警方通過查閱死者的電腦和手機再悼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門核畴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人冲九,你說我怎么就攤上這事谤草。” “怎么了莺奸?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵丑孩,是天一觀的道長。 經(jīng)常有香客問我憾筏,道長嚎杨,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任氧腰,我火速辦了婚禮枫浙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘古拴。我一直安慰自己箩帚,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布黄痪。 她就那樣靜靜地躺著紧帕,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上是嗜,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天愈案,我揣著相機與錄音,去河邊找鬼鹅搪。 笑死站绪,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的丽柿。 我是一名探鬼主播恢准,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼甫题!你這毒婦竟也來了馁筐?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤坠非,失蹤者是張志新(化名)和其女友劉穎敏沉,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體炎码,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡赦抖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了辅肾。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡轮锥,死狀恐怖矫钓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情舍杜,我是刑警寧澤新娜,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站既绩,受9級特大地震影響概龄,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜饲握,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一私杜、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧救欧,春花似錦衰粹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蹬刷,卻和暖如春瓢捉,著一層夾襖步出監(jiān)牢的瞬間频丘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工泡态, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留搂漠,地道東北人。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓兽赁,卻偏偏與公主長得像状答,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子刀崖,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,037評論 2 355