Making UI Automation, will encounter elements in the iframe box, in the position it needs to switch to the iframe, iframe positioning of the position can be positioned by the iframe element, you can also locate index
As follows: index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <center> <a href="index1.html">测试</a> </center> <iframe src="index1.html" name="test"> <iframe src="index1.html" name="test2"> </iframe> </iframe> </body> </html>
index1.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <center id="kt"> 测试课堂 </center> </body> </html>
By positioning element iframe
from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() driver.get("D:\\pycharmwokspace\\UITEST\html\\index.html") iframe_el = driver.find_element_by_name("test") driver.switch_to.frame(iframe_el) print(driver.find_element_by_id("kt").text) driver.quit()
By positioning index
from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() driver.get("D:\\pycharmwokspace\\UITEST\html\\index.html") # iframe_el = driver.find_element_by_name("test") driver.switch_to.frame(0) print(driver.find_element_by_id("kt").text) driver.switch_to.default_content() #切换到最外层 driver.quit()
Nested implementation ideas: first into the first layer, and then enter the second layer, similar wording