Web自动化测试:switch_to包详解:切换handle、frame、alert

之前章节,分别对窗口切换(handle)、frame切换、弹窗(alert)切换做了详细的解释,但是我们在写代码的时候发现,这些方法都被编辑器划伤了一条 I 2 ~ [ r j C横线,但是方法还是可以正常使用,只是目前的pycharm不推荐你继续这样使用L : @ ^ ` { 0 /了(有新的方法可以替代它),那如D _ | L +果我们不使用这些方法的话,我们该怎么去完成切换窗口、frame这些操作呢?所以我们来学习一下替代这几个方法的switch_to包。

一、switch_to包的方法详解
在switch_to的基础上,有这么几? P T Y Z S T个方法,鉴于基本上都是之前曾经讲过的,这次把等价的方法也列出来,p b $ { +供大家参考

driver.switch_to.p# _ x ^ carent_frame()

这是switch_to中独有的方法,可以y U y - m - | ) 9切换到上一层的frame,对于层层嵌套的frame很有用

案例展示:

163邮箱登录的例子来用新的switchG h g Y x T_to方法写一下,并通过观察,我们发现进入D * 5 ( S这个页面后焦点直接就定位到输入框里了,T = m ` @所以我们可以通过active_element()来定位
Web自动化测试:switch_to包详解:切换handle、frame、alert

from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()

# 进入163邮箱首页
driver.get("http://mail.163.com/")
sleep(2)

# 切换到包含登录框的_ ^ + wfrai K ( 0 h | { | ome下
driver.switch_to.frame(1)| ) 7 f # R = }

# 通过定位输当前焦点元素,并再次输入数据
ele_box = d* L S . 4 g ~river.switch_to.active_element
ele_box.send_keys("Q [ &1x 2 w23")
swa $ 8itch_to源码
class SwitchTo:
def init(self, driver):
self._driver = driver

@property
def acF w k ptive_element(self):
"""
Return^ z b Ts the element with focus, oru D | , 1 q BODY[ 8 s y 3 9 v v 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._dri3 : Y } 7 ?ver.execute(, r c , % D h E 2Command.GET_ACTIVE_ELEMENT)['value']
@property
def alert(self):
"""
Switches focus to an alert onm 4 + U Y % u ~ $ the page.
:Usage:
alert = drive! + # E r.switch_to.alert
"""
return Alert(self._driver)
def default_content(self):
"""
Switch focus to the default frame.
:Usage:
driver.switch_to.default_content()
"""&  _ C k K
self._driver.execute(Command.SWITCH_TO_FRAME, {'id': None})
def frame(self, fV } 9 X Q ; 5 qrame_reference):
"""
Switches foc$ _ @ / # E ;us to the specified frame, by inde} : T + = 8 Y ; vx, name, or webelement.
:Args:
- frame_rex k , - J & U !ference: The name of the window to switch to, an im F 9 8 Q F U  _nteger reprq  7 ]esenting the index,
or a webelement that is an (i)frame to switch to.
:Usage:
driver.switch_i p b 8 ~ S I jto.frame('frame_name')g ? 0
driver.switch_to.fra 9 . Ame(1)
driver.switch_to.frame(dw q c + 5 / + y river.f7 B | ! @ 0 % 5ind_elements_by_tag_name("iframe")[0])
"""
if isinstance(framy 2 M b U  ( #e_reference, basestring) and a U self._driver.w3c:
try:
frame_reference = self._driver3 h V $ n P r $.find_, z Q j 6 * ) j XeX { : Y _lement(By6 a x f 8 .II 4 A } . & 6D, frame. O 8 N 4 F_reference)
except NoSuchElementException:
try:
frame_re- - Q m e f ( 6fere J o @nce = self._driver.find_element(By.NAME, frame_reference)
exceS 1 h U } Vpt NoSuchElementException:
raise NoSuchFrameException(frame_reference)
self._driver.execute(Command.SWITCH_TO_FRAME, {'id* y A': frame_reference})
def parent_frame(self):
"""
Switch+ t N . A w s ves focus to the parent context. If the curT . h i ?rent context is the top
level browsing context, the context remains unchanged.
:Us- l 4 b U ( ^ ( Jage:
driver.switch_to.parent_frame()
"""
self._driver.execute(CP : | ; 5 u z xommand.; I g ^ YSWITCH_TO_PARENT_FRAME)
def window(self, windowf K n r_name):
"""; 4 ^
Switches focus to the specified window.
:Args:
- window_name: The name or window hu 1 K H K [ 5andle of the window to switch to.
:Usage:
driver.switch_to.window('main')
"""
data = {'name': win% t : ] [dow_name}
if self._driver.w3c:
data = {N b }'handle': window_name}
self._driver.execute(Command.SWITCH_TO_WINDOW, data)