最近在看python網絡爬蟲,因而我想本身寫一個郵箱和QQ空間的自動登陸的小程序,python
下面以登陸163郵箱和QQ空間和爲例:web
瞭解到在Web應用中常常會遇到frame/iframe 表單嵌套頁面的應用,WebDriver 只能在一個頁面上對元素識別與定位,對於frame/iframe 表單內嵌頁面上的元素沒法直接定位。這時就須要經過switch_to.frame()方法將當前定位的主體切換爲frame/iframe 表單的內嵌頁面中chrome
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 31 2018小程序
@author: chenlinlab
"""
from selenium import webdriver
from time import sleep
options = webdriver.ChromeOptions()
options.add_argument('disable-infobars')
driver = webdriver.Chrome(chrome_options=options)
driver.maximize_window()網絡
#163 email login
driver.get('http://mail.163.com/')
sleep(1)
driver.switch_to.frame("x-URS-iframe")
driver.find_element_by_name("email").clear()
driver.find_element_by_name("email").send_keys('Your163EmailName')
driver.find_element_by_name("password").clear()
driver.find_element_by_name("password").send_keys('Your163EmailPassword')
driver.find_element_by_id("dologin").click()spa
sleep(10)3d
#qq zone login
driver.get('http://qzone.qq.com')
sleep(1)
driver.switch_to_frame('login_frame')
driver.find_element_by_id('switcher_plogin').click()
driver.find_element_by_id('u').clear()
driver.find_element_by_id('u').send_keys('YourQQ')
driver.find_element_by_id('p').clear()
driver.find_element_by_id('p').send_keys('YourQQPassword')
driver.find_element_by_id('login_button').click()blog