python+selenium基础篇,切入切出frame

1、首先制作一个html的文件,代码如下

<!DOCTYPE html>
<html>
<head>
<title>Frame_test</title>
</head>

<div>
    <iframe id="search" src="http://www.sogou.com" width="800" height="500">
    </iframe>
</div>
<div>
    <select name="select" id="selID">
<option value="0">下拉菜单一</option>
<option value="1">下拉菜单二</option>
<option value="2" selected>下拉菜单三</option>
<option value="3">下拉菜单四</option>
<option value="4">下拉菜单五</option>
</select>
<input type="button" id="bt" value="查看效果"/>
</div>

<html>

2、python中使用的代码如下

from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select #导入下拉框的包

dr=webdriver.Firefox()
dr.get("file:///F:/python/UI_51zxw/基础元素定位+显示隐式等待+切入切出frame/frame切入切出/frame_demo.html")#html文件为案例文件
dr.switch_to.frame('search')#切入框架,search是frame的id,是唯一标识(如id,name,class等等等)
# 有唯一标识的情况下填写唯一标识就行了,没有唯一标识的情况下先定位frame
dr.find_element_by_css_selector("#query").send_keys("python")
dr.find_element_by_css_selector("#stb").click()
# dr.switch_to_default_content()#切出框架
dr.switch_to.default_content()#切出框架的另一种写法dr.switch_to.default_content()#切回主文档
# switch_to.parent_frame()#从子frame切回到父frame在frame嵌套中可以使用
select=Select(dr.find_element_by_css_selector('[id="selID"]'))#定位select的框
select.select_by_index(4)#根据元素下标进行定位
sleep(3)
dr.quit()
原文地址:https://www.cnblogs.com/jinbiaobowen/p/13524091.html