之前章節(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)