Selenium多层frame切换操作

假如页面结构为

<frameset>
	<frame id="fr1">
		...
		<frame id="fr2">...</frame>
		...
	</frame>
	...
</frameset>

frameset 不用切换,但在frame中操作元素需要切换。

切换frame可以用:index、id、name 、webelement任意种方式切换。

源码描述:
"""
        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])
        """

要操作id=“fr1”中的元素,则需先切换,eg:

driver.switch_to.frame("fr1")

操作完后需退出当前的frame,eg:

driver.switch_to.default_content()

操作id="fr2"中的元素,则需要一层一层切换,eg:

driver.switch_to.frame("fr1")
driver.switch_to.frame("fr2")

在此,fr2中操作完后,还要操作fr1中元素的话,则使用

driver.switch_to.parent_frame()

切换到上级的frame;
此时,当前还是在fr1中,则需要再次切换,切换到主文档的元素

driver.switch_to.default_content()

猜你喜欢

转载自blog.csdn.net/cz9025/article/details/83013261
今日推荐