selenium实现模拟登录
2018-08-09 本文已影响0人
tkpy
selenium实现模拟登录 , 发帖
# 实现模拟登录
def bitalk_log(username,password):
# 请求登录页面
try:
drive = webdriver.Chrome()
url = 'https://steemconnect.com/oauth2/authorize?client_id=duozhuan&redirect_uri=https%3A%2F%2Fwww.bitalk.cc%2Fcallback%2F&scope='
drive.get(url)
# 睡眠2秒
time.sleep(2)
# 找到continue按钮点击进去
drive.find_element_by_xpath("//button").click()
# 休眠0.5秒钟后执行填写用户名和密码操作
time.sleep(0.5)
# 找到用户名输入用户名
user = drive.find_element_by_id("username")
user.send_keys(username)
# 找到密码输入密码
drive.find_element_by_id("password").send_keys(password)
# 点击登录按钮实现登录
drive.find_element_by_xpath("//button").click()
# 登录成功后跳转首页,进行加载,休眠10秒加载页面
time.sleep(20)
# 点击进入发帖页面
# drive.switch_to_default_content()
cookies_list = drive.get_cookies()
print(cookies_list)
return drive
except Exception as e:
print("出现问题",e)
# 实现发帖
def fatie(drive,json):
global n
global nn
data = json
drive2 = drive
# 标题
title = json['title']
# 主题
topics = json['topics']
# 内容
body = json['content']
#图片
files = json['files']
try:
drive.find_element_by_xpath("//div[@class='right']//ul/li[1]/a").click()
time.sleep(5)
try:
# 标题
drive.find_element_by_id('title').send_keys(title)
# 主题
for i in topics:
drive.find_element_by_id('topics').send_keys(i)
time.sleep(3)
drive.find_element_by_id('title').click()
# 上传图片
for i in files:
drive.find_element_by_id('editor-inputfile').send_keys(i)
time.sleep(4)
# 添加内容
# drive.find_element_by_id('body').send_keys(body)
# 奖励
# 先定位到下拉框,点击
# # 需要奖励的选项
# a = json['a']
# drive.find_element_by_xpath("//div[@class='ant-select ant-select-enabled']").click()
# time.sleep(0.5)
# # 点击下拉框下的选项
# if a == 1:
# drive.find_element_by_xpath("//body/div[5]//ul/li[@unselectable='unselectable'][1]").click()
# time.sleep(0.5)
# elif a == 2:
# drive.find_element_by_xpath("//body/div[5]//ul/li[@unselectable='unselectable'][2]").click()
# time.sleep(0.5)
# elif a == 3:
# drive.find_element_by_xpath("//body/div[5]//ul/li[@unselectable='unselectable'][3]").click()
# time.sleep(0.5)
# 不喜欢这篇文章
b = json['b'] # 1表示不喜欢
if b == 1:
drive.find_element_by_xpath("//input[@id='upvote']").click()
# 草稿进行保存睡眠4秒
time.sleep(4)
# 发布
drive.find_element_by_xpath("//button[@class='Action Action--big Action--primary']").click()
# 点击发布跳转加载页面
time.sleep(10)
title1 = drive.find_element_by_xpath("//div[@class='StoryFull']/h1[@class='StoryFull__title']").text
if title1 == title:
print('发布成功')
return False