[python自动化]selenium实现超星某某通自动评论

2021/6/18 20:27:26

本文主要是介绍[python自动化]selenium实现超星某某通自动评论,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录

  • 思路
  • 代码封装
  • 效果

思路

  1. 使用selenium提供的函数实现标签的定位与页面的切换。
  2. 编写js代码找上者无法定位的标签(jQuery简单应用)
  3. 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)

效果

在这里插入图片描述



这篇关于[python自动化]selenium实现超星某某通自动评论的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程