from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
option = webdriver.ChromeOptions()
option.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=option)
driver.maximize_window()
driver.implicitly_wait(5)
driver.get("http://i.chaoxing.com/")
# 登录学习通
driver.find_element(By.ID, "phone").send_keys("")
driver.find_element(By.ID, "pwd").send_keys("")
driver.find_element(By.ID, "loginBtn").click()
# ----------------------------------------------------------------------
# 多表单的切换
# 等待 ID 为 frame_content 的iframe表单可用
wait = WebDriverWait(driver, 10)
frame = wait.until(EC.presence_of_element_located((By.ID, "frame_content")))
def switch_to_frame(driver, by, value):
while True:
try:
frame = driver.find_element(by, value)
driver.switch_to.frame(frame)
break
except StaleElementReferenceException:
continue
# 切换到 frame_content 的iframe表单中
switch_to_frame(driver, By.ID, "frame_content")
# 获取添加课程的文本信息
text = driver.find_element(By.ID, "addCourse").text
print(text)
# 回退到上一层的frame
driver.switch_to.parent_frame()
text2 = driver.find_element(By.ID, "siteName").text
print(text2)
# 重新查找并切换到 frame_content 的iframe表单中
switch_to_frame(driver, By.ID, "frame_content")
# 回退到最外层的frame
driver.switch_to.default_content()
text3 = driver.find_element(By.ID, "siteName").text
print(text3)
标签:python,frame,driver,表单,content,find,element,ID,页面
From: https://blog.csdn.net/weixin_45953140/article/details/139244461