driver.switch_to.frame(reference) # 切换到子 frame
driver.switch_to.parent_frame() # 切回到父 frame
driver.switch_to.default_content() # 切回主文书档案
frame标签有(frameset、frame、iframe)三种,个中frameset为普遍标签,不会感化到平常的定位。
而frame与iframe对selenium定位而言是一律的,selenium有一组本领对frame举行操纵。
switch_to.frame(reference)
reference是传入的参数,用来定位frame,不妨传入id、name、index以及selenium的WebElement东西
▶ 示例:
<html lang="en">
<head>
<title>FrameTest</title>
</head>
<body>
<iframe src="a.html" id="frame1" name="myframe"></iframe>
</body>
</html>
▶ 定位个中的iframe并切进去(对于无 id和name 这两项属性,则不妨用index和WebElement来定位)
from selenium import webdriver
driver = webdriver.Firefox()
driver.switch_to.frame(0) # 1.index 定位,从0发端
driver.switch_to.frame("frame1") # 2.用id 定位
driver.switch_to.frame("myframe") # 3.用name 定位
driver.switch_to.frame( # 4.用WebElement东西定位(本领:tag_name,xpath)
driver.find_element_by_tag_name("iframe"))
driver.switch_to.frame(
driver.find_element_by_xpath("//iframe[contains(@src,'myframe')]"))
switch_to.default_content()从 frame 中切回主文书档案
切到frame中之后,咱们若像连接操作东文书档案的元素,则需切回主文书档案
driver.switch_to.default_content()
switch_to.parent_frame() 嵌套frame的操纵
示例:
<html>
<iframe id="frame1">
<iframe id="frame2" / >
</iframe>
● 从主文书档案切到frame2,一层层切进去
driver.switch_to.frame("frame1")
driver.switch_to.frame("frame2")
● 从frame2再切回frame1(子frame切回到父frame)
driver.switch_to.parent_frame() # 即使暂时已是主文书档案,则失效果