selenium实例:python登录webqq

selenium范例:python登录webqq
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
from pprint import pprint 

username="username"
passwd="password"

class WebQQ:
  def __init__(self):
    self.browser = webdriver.Firefox() # Get local session of firefox
    self.browser.set_window_position(800,0)
    self.browser.get("http://web.qq.com") # Load page
    while True:
      try:
        elem = self.browser.find_element_by_id("alloy_icon_app_50_3") # Find the qq button
        elem.click()
        print "click qq button"
        break
      except NoSuchElementException:
        print "element not loaded yet"
        time.sleep(0.1) # Let the page load, will be added to the API
  def login(self):
    self.browser.switch_to_frame("ifram_login")
    # Find the loginState option 
    elem = self.browser.find_element_by_id("loginState") 
    elem.click()
    print "click loginState"
    # choose hidden state
    elem = self.browser.find_element_by_id("loginStatePanel") 
    elems = elem.find_elements_by_class_name("statePanel_li")
    elems[0].click()
    print "choose online state"
    # login
    elem = self.browser.find_element_by_id("u") # user name
    elem.send_keys(username)
    elem = self.browser.find_element_by_id("p") # passwd 
    elem.send_keys(passwd)
    elem = self.browser.find_element_by_id("login_button") # login button
    elem.click()

def main():
  webqq = WebQQ()
  webqq.login()

if __name__ == "__main__":
  main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值