#csdn自动签到
(1)首先进入官网
<img src="https://img-blog.csdnimg.cn/20200818163240304.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RhbnN0eV96aA==,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"> (2)点击登录 <img src="https://img-blog.csdnimg.cn/20200818163307974.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RhbnN0eV96aA==,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述">
(1)首先需要点击账号密码登录 <img src="https://img-blog.csdnimg.cn/20200818163411610.png#pic_center" alt="在这里插入图片描述">
构造语句对元素进行查找
driver.find_element_by_link_text("账号密码登录").click()
<img src="https://img-blog.csdnimg.cn/20200818163506754.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RhbnN0eV96aA==,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述">
用户名:
<img src="https://img-blog.csdnimg.cn/20200818163520241.png#pic_center" alt="在这里插入图片描述">
密码:
<img src="https://img-blog.csdnimg.cn/20200818163536872.png#pic_center" alt="在这里插入图片描述">
构造相应的语句,使用css选择器进行选择
driver.find_element_by_css_selector("[placeholder='手机号/邮箱/用户名']").send_keys(user)
driver.find_element_by_css_selector("[placeholder='密码']").send_keys(password)
成功输入 (4)点击登录按钮
<img src="https://img-blog.csdnimg.cn/20200818163555584.png#pic_center" alt="在这里插入图片描述">
driver.find_element_by_css_selector("button").click()
点击进入到
<img src="https://img-blog.csdnimg.cn/20200818163609117.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RhbnN0eV96aA==,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述">
(1)这里发现点击头像会跳转到个人中心,直接构造函数访问新的网页
new_window='window.open("{}")'.format("https://i.csdn.net/#/uc/profile")#js函数,此方法适用于所有的浏览器
driver.execute_script(new_window)
成功进入
<img src="https://img-blog.csdnimg.cn/202008181636348.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RhbnN0eV96aA==,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"> (2)跳转到签到页面
我在这里发现每个按钮的网页链接会不一样,因此我直接用js跳转到新的网页
new_window = 'window.open("{}")'.format("https://i.csdn.net/#/uc/reward") # js函数,此方法适用于所有的浏览器
driver.execute_script(new_window)
<img src="https://img-blog.csdnimg.cn/20200818163723581.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RhbnN0eV96aA==,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"> <img src="https://img-blog.csdnimg.cn/20200818163743948.png#pic_center" alt="在这里插入图片描述">
经过实验发现签到和完成签到的class属性不一样
未签到是:handle_box to_sign 签到完成是:handle_box has_sign 可以抽奖是:handle_box to_reward 构造代码
try:
elem=driver.find_element_by_xpath("//div[@class='handle_box has_sign']")
except:
print("您有可能还未签到或可以抽奖")
else:
messagebox.showinfo("错误", "您已经签到过了")
driver.quit()
sys.exit(1)
#如果已经完成签到就退出
try:
elem=driver.find_element_by_xpath("//div[@class='handle_box to_reward']")
except:
print("您还未签到")
else:
messagebox.showinfo("恭喜", "您可以去抽奖了")
driver.quit()
sys.exit(1)
#提示可以抽奖
try:
elem=driver.find_element_by_xpath("//div[@class='handle_box to_sign']")
except:
messagebox.showinfo("错误", "没有找到对应的元素")
driver.quit()
sys.exit(1)
else:
elem.click()
成功完成签到
find_element_by_class_name:根据class定位
find_element_by_css_selector:根据css定位
find_element_by_id:根据id定位
find_element_by_link_text:根据链接的文本来定位
find_element_by_name:根据节点名定位
find_element_by_partial_link_text:根据链接的文本来定位,只要包含在整个文本中即可
find_element_by_tag_name:通过tag定位
find_element_by_xpath:使用Xpath进行定位
PS:把element改为elements会定位所有符合条件的元素,返回一个List
比如:find_elements_by_class_name
返回的是web_element对象
如果跳转到新的网页需要对浏览器窗口进行切换,要不然他还是在原网页查找,会报错
for handle in driver.window_handles:
driver.switch_to.window(handle)
if "个人" in driver.title:
break
回到原来
# mainWindow变量保存当前窗口的句柄
mainWindow = wd.current_window_handle
这里是先保存现在网页的handle,方便之后的返回
new_window = 'window.open({}")'.format("https://i.csdn.net/#/uc/reward") # js函数,此方法适用于所有的浏览器
driver.execute_script(new_window)
本项目所采取的方法:
def set_init_window(self):
self.init_window_name.title("CSDN自动签到") #标题
self.init_window_name.geometry() #设置窗口大小,设置窗口位置
self.init_label=Label(self.init_window_name,text="账号: ")
self.init_label.grid()#x=0,y=0
self.user=Entry(self.init_window_name) #创建输入框
self.user.grid(row=0,column=1)
self.init_label2=Label(self.init_window_name,text="密码: ")
self.init_label2.grid(row=1,column=0)
self.password=Entry(self.init_window_name)
self.password.grid(row=1,column=1)
self.w=Button(text="签到", bg="lightblue", width=10,command=self.driver)
self.w.grid()
tk库的具体使用:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。