- frame:frame是html中的框架,即在一個瀏覽器中顯示多個頁面,frame分為水平框架和垂直框架。
- frame標簽包含 frame iframe frameset
測試網(wǎng)頁: https://www.w3school.com.cn/tiy/t.asp?f=html_frame_cols
垂直frame
<html>
<frameset cols="25%,50%,25%">
<frame src="/example/html/frame_a.html">
<frame src="/example/html/frame_b.html">
<frame src="/example/html/frame_c.html">
</frameset>
</html>
水平frame
<html>
<frameset rows="25%,50%,25%">
<frame src="/example/html/frame_a.html">
<frame src="/example/html/frame_b.html">
<frame src="/example/html/frame_c.html">
</frameset>
</html>
frame存在兩種:嵌套,非嵌套
切換frame的方法:
根據(jù)元素id或index切換frame:driver.switch_to.frame()
切換到默認frame:driver.switch_to.default_content()
切換到父級frame:driver.switch_to.parent_frame()
切到frame頁:
1.處理未嵌套的frame:
driver.switch_to_frame("frame的id")
driver.switch_to_frame("frame-index")frame無ID時依據(jù)索引來處理,索引從0開始driver.switch_to_frame(0)
2.處理嵌套frame:
對于嵌套的先進入到iframe的父節(jié)點,再進到子節(jié)點,然后可以對子節(jié)點里面的對象進行處理和操作
driver.switch_to.frame("父節(jié)點")
driver.switch_to.frame("子節(jié)點")
從frame切回頁面
switch_to.parent_frame()
switch_to.default_content()
測試頁面:https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable
以獲取frame頁面元素為例:
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestFrame:
class TestWindows():
def setup(self):
self.driver = webdriver.Chrome()
# 隱式等待
self.driver.implicitly_wait(5)
# 窗口最大化
self.driver.maximize_window()
self.driver.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
def teardown(self):
self.driver.quit()
def test_frame(self):
# 切換到frame,兩種方式都可行
self.driver.switch_to.frame('iframeResult')
# self.driver.switch_to_frame('iframeResult')
# 獲取frame上的元素
print(self.driver.find_element(By.XPATH, '//*[@id="draggable"]').text)
# 從frame切回頁面,兩種方式
#self.driver.switch_to.parent_frame()
self.driver.switch_to.default_content()
sleep(2)
# 獲取頁面上的元素,獲取頁面上點擊運行按鈕
self.driver.find_element(By.XPATH, '//*[@id="submitBTN"]').click()