selenium 中多frame 定位元素
想要了解这个知识的的朋友们,必须要有html中的框架基础,明白框架的的作用,不理解的朋友可以去http://www.runoob.com/tags/tag-frame.html先明白框架再往下看
在 web 应用中经常会出现 frame 嵌套的应用,比如在A页面中嵌入页面B,这个时候需要在A页面和B页面之间切换,响应去定位不同的元素,对应刚接触selenium的朋友来说,在这方面肯定出过不少的问题,下面本人在下面总结下相关的问题,希望对大家有帮助。
页面 framedemo.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>主页内容</h2>
<input id="maininput" >
<iframe src="index.html" width="100%" height="400px" id="indexframe" >
</iframe>
</body>
</html>
页面 login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<p>=========登录=======</p>
<form action="success.html" method="post">
用户名:<input type="text" id="username" name="username" class="test"><br/>
密码:<input type="password" id="userpw" name="userpw" class="test"><br/>
<input type="submit" value="登录" id="login">
</form>
</body>
</html>
在 framedemo.html中嵌入login.html网页
下面我们想在framedemo.html页面中定位id 为 maininput 页面元素并输入值,再定位login.html中id为username的页面元素并输入值,代码如下:
from selenium import webdriver
import os
import time
fox = webdriver.Firefox()
fox.get("file:///"+os.path.abspath("framedemo.html"))
fox.find_element_by_id("maininput").send_keys("这个是主页")
time.sleep(2)
fox.find_element_by_id("username").send_keys("admin")
time.sleep(2)
fox.quit()
运行代码结果如下:
Traceback (most recent call last):
File "E:/pythonproject/seleniumdemo/qhcs/frame.py", line 12, in <module>
fox.find_element_by_id("username").send_keys("admin")
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium-3.4.0-py3.6.egg\selenium\webdriver\remote\webdriver.py", line 282, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium-3.4.0-py3.6.egg\selenium\webdriver\remote\webdriver.py", line 784, in find_element
'value': value})['value']
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium-3.4.0-py3.6.egg\selenium\webdriver\remote\webdriver.py", line 249, in execute
self.error_handler.check_response(response)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium-3.4.0-py3.6.egg\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="username"]
出现异常NoSuchElementException: Message: Unable to locate element: [id=”username”] 言而之意是定位不到id=”username”元素,这个时候需要用到 fox.switch_to.frame(“indexframe”) 方法,才可以获取到执行通过
fox.switch_to.frame(“indexframe”) 详细使用可以去看源码
:Usage:
driver.switch_to.frame('frame_name')#根据frame的id 或者名字
driver.switch_to.frame(1)#根据第一个frame
driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
所以修改代码如下:
from selenium import webdriver
import os
import time
fox = webdriver.Firefox()
fox.get("file:///"+os.path.abspath("frame.html"))
fox.find_element_by_id("maininput").send_keys("这个是主页")
time.sleep(2)
#需要切换frame才可以定位元素
fox.switch_to.frame("indexframe")
fox.find_element_by_id("username").send_keys("admin")
time.sleep(2)
fox.quit()
再次执行代码发现可以定位username元素并输入admin的值,举例说明
比如在当前的房间里只能看到当前房间的物体,如果想要看到其他房间的物体,必须要进入其他的房间
如果有这样的需要 现在需要返回到在frmedemo.html页面再次定位元素,该怎么办?
代码如下:
from selenium import webdriver
import os
import time
fox = webdriver.Firefox()
fox.get("file:///"+os.path.abspath("frame.html"))
fox.find_element_by_id("maininput").send_keys("这个是主页")
time.sleep(2)
#需要切换frame才可以定位元素
fox.switch_to.frame("indexframe")
fox.find_element_by_id("username").send_keys("admin")
time.sleep(2)
#返回主的frame需 要切换 ,然后再次定位主页面的值。
fox.find_element_by_id("maininput").clear()
fox.find_element_by_id("maininput").send_keys("再次输入主页的值")
time.sleep(2)
fox.quit()
运行结果如下:
C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\python.exe E:/pythonproject/seleniumdemo/qhcs/frame.py
Traceback (most recent call last):
File "E:/pythonproject/seleniumdemo/qhcs/frame.py", line 17, in <module>
fox.find_element_by_id("maininput").clear()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium-3.4.0-py3.6.egg\selenium\webdriver\remote\webdriver.py", line 282, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium-3.4.0-py3.6.egg\selenium\webdriver\remote\webdriver.py", line 784, in find_element
'value': value})['value']
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium-3.4.0-py3.6.egg\selenium\webdriver\remote\webdriver.py", line 249, in execute
self.error_handler.check_response(response)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium-3.4.0-py3.6.egg\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="maininput"]
要解决这个问题修改代码如下:
from selenium import webdriver
import os
import time
fox = webdriver.Firefox()
fox.get("file:///"+os.path.abspath("frame.html"))
fox.find_element_by_id("maininput").send_keys("这个是主页")
time.sleep(2)
#需要切换frame才可以定位元素
fox.switch_to.frame("indexframe")
fox.find_element_by_id("username").send_keys("admin")
time.sleep(2)
#返回主的frame需 要切换 ,然后再次定位主页面的值。
fox.switch_to.parent_frame()
fox.find_element_by_id("maininput").clear()
fox.find_element_by_id("maininput").send_keys("再次输入主页的值")
time.sleep(2)
fox.quit()
添加代码fox.switch_to.parent_frame() 含义是,切换到主的frame中才可以再次定位元素。
希望对大家有帮助!!谢谢