如何在 Python 中的 Selenium Webdriver 中處理框架?
我們可以在 Python 的 Selenium webdriver 中處理框架。iframe 在 html 文件中用 <iframe> 標記標識。iframe 是包含位於另一個 html 文件中元素的 html 文件。讓我們看看一個框架的 html 文件。
以下方法有助於在 iframe 之間切換 −
switch_to.frame(args) – 框架索引作為引數放入方法中。iframe 的開始索引為 0。
語法
driver.switch_to.frame(0),切換到第一個 iframe。
switch_to.frame(args) - 框架名稱或 ID 作為引數放入方法中。
語法
driver.switch_to.frame("nm"),切換到名為 nm 的 iframe。
- switch_to.frame(args) - 框架 web 元素作為引數放入方法中。
語法
driver.switch_to.frame(f),切換到帶 web 元素 f 的 iframe。
switch_to.default_content() – 從 iframe 切換到父頁面。
語法
driver.switch_to.default_content()
示例
實現程式碼。
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.get("https://the-internet.herokuapp.com") driver.find_element_by_link_text("Frames").click() driver.find_element_by_link_text("Nested Frames").click() # switch to frame with name driver.switch_to.frame("frame-bottom") # identify the element and get text method s = driver.find_element_by_xpath("//body").text print ("Test inside frame: " + s) # move out of frame to the parent page driver.switch_to.default_content() driver.quit()
輸出
廣告