思路
- 使用selenium提供的函数实现标签的定位与页面的切换。
- 编写js代码找上者无法定位的标签(jQuery简单应用)
- selenium执行js代码
代码封装
from selenium import webdriver
from time import sleep
class AutoComment:
js = "let submitEle=$('.qdBtn');" \
"submitEle.click();"
def __init__(self, phone, password):
self.phone = phone
self.password = password
def start(self, path, name):
"""
开始评论
:param name: 课程名
:param path: 本地Edge浏览器驱动的路径
:return:
"""
b = webdriver.Edge(path)
b.get('https://passport2.chaoxing.com/login?fid=&newversion=true&refer=http%3A%2F%2Fi.chaoxing.com')
b.maximize_window()
b.find_element_by_id('phone').send_keys(self.phone)
b.find_element_by_id('pwd').send_keys(self.password)
b.find_element_by_id('loginBtn').click()
sleep(2)
b.switch_to.frame('frame_content')
b.find_element_by_link_text(name).click()
windows = b.window_handles
b.switch_to.window(windows[-1])
b.find_element_by_link_text('讨论').click()
for i in range(50):
title = b.find_element_by_id('c_title')
title.click()
title.send_keys(i)
b.execute_script(self.js)
b.switch_to.alert.accept()
sleep(2)
效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210618184751985.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dlbmVyYWxfenk=,size_16,color_FFFFFF,t_70#pic_center)