WebDriver多表单切换
""" web中经常遇到frame/iframe表单嵌套页面的应用,WebDriver只能在一个页面上对元素识别与定位,对于内嵌页面无法定位识别到。 需要使用到switch_to.frame()方法将当前定位主体切换到内嵌页面上。 """ from selenium import webdriver from selenium.webdriver.common.by import By import time import os driver = webdriver.Chrome() file_path = "file:///" + os.path.abspath("frame.html") driver.get(file_path) # 切换到iframe:嵌套的csdn页面 driver.switch_to.frame("iframe01") # 对嵌套的csdn页面进行搜索操作 driver.find_element(By.ID, "toolbar-search-input").send_keys("python知多少?") driver.find_element(By.ID, "toolbar-search-button").click() time.sleep(3) driver.quit()
#frame.html代码 frame or iframe frame