Today, let's learn how to use selenium to realize CSDN automatic sign-in, let's start learning!
1. Log in
I won't say much about login, in my Python extra : Use selenium to automatically log in to the CSDN article.
2. Automatic sign-in
2.1 HTML source code of sign-in button
First, we visit the sign-in page and find the HTML source code of the sign-in button:
When not checked in:
you can see that when the button is not checked in, there is a div tag whose class attribute is handle_box to_sign.
When checked in: When
checked in, the button is a label with the class attribute of handle_box has_sign.
2.2 Writing code
from selenium.webdriver import Firefox
from time import sleep
def csdn_login(driver, username, password):
# 省去登录代码 可以去复制我的那篇文章
# 把executable_path你电脑里的浏览器驱动
driver = Firefox(executable_path="geckodriver.exe")
# 登录
csdn_login(driver, "你的用户名", "你的密码")
# 等待3秒 为了等服务器上传登录信息
sleep(3)
# 访问签到页面
driver.get("https://i.csdn.net/#/user-center/draw/")
# 如果存在未签到的div按钮
try:
div = driver.find_element_by_xpath("//div[@class='handle_box to_sign']")
except NoSuchElementException:
# 则打印已经签到了
print("已经签到了!")
else:
# 否则点击div按钮
div.click()
print("签到完成!")
# 关闭页面
driver.close()
Run the code and you can automatically sign in!
Today’s course is over, if you are interested, you can bookmark it and like it, thank you!