python使用selenium模拟登录网易

#-*-coding:utf-8-*-
from selenium import webdriver
import time
import json

name = input("输入登录名:")
pwd  = input("输入密码:")
#name = "******"
#pwd = "******"
print(name+":"+pwd)

driver = webdriver.Chrome()     # 创建Chrome对象.
driver.maximize_window()
driver.implicitly_wait(10)
# 操作这个对象.
driver.get('http://mp.163.com/')     # get方式访问
time.sleep(5)
iframe = driver.find_element_by_tag_name("iframe")
#print("name===="+str(iframe))
#driver.switch_to_frame(iframe)
driver.switch_to.frame(iframe)
#driver.switch_to_frame
#el = driver.find_element_by_xpath("//input[@name='email']")
el = driver.find_element_by_name("email")
el.send_keys(name)
time.sleep(2)
#el = driver.find_element_by_xpath("//input[@type='password']")
el = driver.find_element_by_name("password")
el.send_keys(pwd)
time.sleep(2)

el = driver.find_element_by_id("dologin")
el.click()
time.sleep(2)

#获取网站cookie
diccookie=driver.get_cookies()
fw=open('wangyicookie.txt','w')
json.dump(diccookie,fw)
fw.close()

el = driver.find_element_by_class_name("menu-item-sub")
el.click()
time.sleep(2)

#滚动到浏览器顶部
js_top = "var q=document.documentElement.scrollTop=0"
#滚动到浏览器底部
js_bottom = "var q=document.documentElement.scrollTop=10000"
js_bottom2 = "window.scrollTo(0,document.body.scrollHeight)"


for i in range(1,5):
    try:
        driver.execute_script(js_top)
        el = driver.find_elements_by_class_name("ne-input-mask-content")[1]
        el.click()
        time.sleep(2)
        el = driver.find_element_by_xpath('//div[@class="ne-drop-down-menu-content"]/ul/li[4]')
        el.click()
        time.sleep(5)
        el = driver.find_element_by_xpath('//button[@class="box-trigger"]')
        el.click()
        time.sleep(2)

        el = driver.find_element_by_class_name("tool-tip-btn")
        el.click()
        time.sleep(12)
        #driver.refresh()
    except:
        pass
    

#print(txt)

#driver.get_screenshot_as_file('foo.png')
time.sleep(10)
#driver.quit()   # 使用完, 记得关闭浏览器, 不然chromedriver.exe进程为一直在内存中.

 

posted @ 2020-08-25 10:41  凉城  阅读(876)  评论(0编辑  收藏  举报