web自动化之iframe操作
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By import time driver = webdriver.Chrome() driver.get("https://ke.qq.com/") driver.maximize_window() driver.find_element_by_xpath('//a[text()="登录"]').click() # 选择账号类型 name = (By.XPATH,'//a[text()="QQ登录"]') WebDriverWait(driver,20).until(EC.presence_of_element_located(name)) driver.find_element_by_xpath('//a[text()="QQ登录"]').click() # 切换iframe需要传的参数,元祖类型,也可以是一个webelement 对象,也可以是iframe的name 或者di属性 ment = (By.XPATH,'//iframe[@name="login_frame_qq"]') # 等待iframe出现,并切入进去 WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it(ment)) time.sleep(2) # 定位元素 driver.find_element_by_xpath('//span[@class="img_out_focus"]').click() # 退出iframe driver.switch_to_default_content()