来源:python中国网 时间:2020-03-17

  本文的代码仅限于测试网页邮箱首页的iframe标签操作,并不是要自动登录邮箱(自动登录也不难!)。

  1 打开网易邮箱首页

  2 切换到iframe标签-通过元素定位的方式定位到iframe再切换

  3 登录按钮所在的html标签

# -*- coding: utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options


def go_click(url):
    try:
        driver.get(url)
        # 加载到底部
        foot = WebDriverWait(driver, 15).until(
            EC.presence_of_element_located((By.CLASS_NAME, "footer-main"))
        )
        iframe = driver.find_element_by_css_selector('#loginDiv iframe')
        driver.switch_to.frame(iframe)
        login = driver.find_element_by_id('dologin')
        print('html标签是',login.tag_name)

    except Exception as e:
        print(e)
    else:
        print("ok")
    finally:
        time.sleep(8)
        driver.quit()


if __name__ == "__main__":
    option = Options()
    option.add_argument('--start-maximized')
    option.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
    driver = webdriver.Chrome(options=option)

    go_click("https://mail.163.com/")






html标签是 a
ok


selenium教程

Tips:不要怕、多坚持、多动手、付出必有回报。