文件:frame.html
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"/> <script type="text/javascript">$(document).ready(function(){});> </script> </head> <body> <div class="row-fluid"> <div class="span10 wel"> <h4>frame</h4> <iframe id="if" name="nf" src="http://www.baidu.com" width="800" height="300"> </iframe> </div> </div> </body> <script src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"> </script> </html>
文件:test11.py
from selenium import webdriver
import os,time
driver = webdriver.Chrome()
file_path=’file:///’+os.path.abspath(‘frame.html’)
driver.get(file_path)
#由于使用的是iframe,内嵌,因此需切换到iframe(id=”if”)
from selenium import webdriver import os,time driver = webdriver.Chrome() file_path='file:///'+os.path.abspath('frame.html') driver.get(file_path) #由于使用的是iframe,内嵌,因此需切换到iframe(id="if") driver.switch_to.frame("if") time.sleep(3) driver.find_element_by_id("kw").send_keys("selenium") driver.find_element_by_id("su").click() time.sleep(3) driver.quit()
swith_to_frame()默认直接获取表单的id或name属性,若iframe无可用的id活name,则可以通过下述方法进行定位:
xf=driver.find_element_by_path('//*[@class="if"]') driver.switch_to.frame(xf) driver.switch_to_.parent_frame()
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/193129.html