用python自动登录博客园


from selenium import webdriver
import time
a=webdriver.Chrome()
a.get("http:\\www.baidu.com")
a.find_element_by_xpath("//*[@id='kw']").send_keys("博客园")
a.find_element_by_id('su').click()
time.sleep(1)
a.find_element_by_xpath('//*[@id="1"]/h3/a[1]').click()
time.sleep(1)
l=a.window_handles
a.switch_to_window(l[-1])
a.find_element_by_xpath('//*[@id="span_userinfo"]/a[1]').click()
time.sleep(1)
a.switch_to_window(l[-1])
a.find_element_by_xpath('//*[@id="LoginName"]').send_keys("用户名")
a.find_element_by_xpath('//*[@id="Password"]').send_keys("密码")
a.find_element_by_xpath('//*[@id="IsRemember"]').click()
time.sleep(1)
a.switch_to_window(l[-1])
a.find_element_by_xpath('//*[@id="submitBtn"]').click()
time.sleep(1)
a.switch_to_window(l[-1])
'''
PS:
# 导入驱动和时间模块
from selenium import webdriver
import time
# F12进入开发者模式,可查看需要跳转时元素的位置(元素定位)
#xpath 中填写的为定位元素的位置(可拷贝)
#send_keys中填写要搜索或输入的内容
#time.sleep(秒数)强制等待时间
# 切换窗口,l为列表
l=a.window_handles
a.switch_to_window(l[-1])
'''
posted @ 2019-09-04 19:45  胜者万里也称王  阅读(291)  评论(0编辑  收藏  举报