【老師見打系列】:我只是寫了一個自動回覆討論的指令碼~

專注的阿熊發表於2022-11-07

from selenium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By

import json

import time

class MOOC(object):

     def __init__(self):

         self.driver = webdriver.Chrome()

         self.driver.get(')

     def login(self):

         # 獲取儲存下的 cookie

         with open('moocCookie.txt', 'r', encoding='utf-8') as f:

             listCookies = json.loads(f.read())

         # driver 裡新增 cookie

         for cookie in listCookies:

             cookie_dict = {

                 'domain': '.icourse163.org',

                 'name': cookie.get('name'),

                 'value': cookie.get('value'),

                 'expires': '',

                 'path': '/',

                 'httpOnly': False,

                 'HostOnly': False,

                 'Secure': False

             }

             self.driver.add_cookie(cookie_dict)

         self.driver.refresh() # 重新整理頁面

     # 進入課程

     def entering_the_course(self):

         # 等待“個人中心”元素出現

         WebDriverWait(self.driver, 1000).until(

             EC.presence_of_element_located((By.XPATH, '//*[@id="j-indexNav-bar"]/div/div/div/div/div[7]/div[3]/div/div/a/span'))

         )

         self.driver.find_element(By.XPATH, '//*[@id="j-indexNav-bar"]/div/div/div/div/div[7]/div[3]/div/div/a/span').click()

         # 等待“ SPOC 課程 12 ”元素出現並點選

         time.sleep(1)

         WebDriverWait(self.driver, 1000).until(

             EC.presence_of_element_located((By.XPATH, '//div[@class="u-selectTab-container"]/div[2]/a'))

         )

         self.driver.find_element(By.XPATH, '//div[@class="u-selectTab-container"]/div[2]/a').click()

         # 等待課程元素出現,並獲取元素,使用者選擇課程

         WebDriverWait(self.driver, 1000).until(

             EC.presence_of_element_located((By.XPATH, '//div[@class="course-card-wrapper"]/div/a/div/img'))

         )

         course_ele = self.driver.find_elements(By.XPATH, '//div[@class="course-card-wrapper"]/div/a/div/img')

         course_name = [ele.get_attribute('alt') for ele in course_ele]

         for index, i in enumerate(course_name):

             print(index, i)

         user = int(input(' 請輸入課程式號: '))

         course_ele[user].click()

         time.sleep(2)

         self.driver.switch_to.window(self.driver.window_handles[-1]) # 切換視窗

         time.sleep(2)

         # 等待課件出現並點選

         WebDriverWait(self.driver, 1000).until(

             EC.presence_of_element_located((By.XPATH, '//li[@data-name=" 課件 "]'))

         )

         self.driver.find_element(By.XPATH, '//li[@data-name=" 課件 "]').click()

         # 等待章節元素出現並點選

         WebDriverWait(self.driver, 1000).until(

             EC.presence_of_element_located((By.XPATH, '//div[@class="m-learnChapterList f-pr"]/div[@class="m-learnChapterNormal f-pr"][1]'))

         )

         # 點選章節

         self.driver.find_element(By.XPATH, '//div[@class="m-learnChapterList f-pr"]/div[@class="m-learnChapterNormal f-pr"][1]').click()

         time.sleep(0.3)

         # 點選進入影片

         self.driver.find_elements(By.XPATH, '//div[@class="sourceList"]/div[1]')[0].click()

         # 獲取章節中”選擇課程“的元素並點選

         WebDriverWait(self.driver, 1000).until(

             EC.presence_of_element_located(

                 (By.XPATH, '//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div'))

         )

         # 獲取所有章節

         temp_chapters = self.driver.find_elements(By.XPATH, '//div[@class="f-fl j-chapter"]//div[@class="down f-bg j-list"]/div')

         # 透過迴圈獲取到的章節元素和課程元素,進行討論

         for chapter_index in range(len(temp_chapters)): # 迴圈章節

             # 獲取章節中”選擇課程“的元素並點選

             WebDriverWait(self.driver, 1000).until(

                 EC.presence_of_element_located(

                     (By.XPATH, '//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div'))

             )

             # 獲取章節選項框並點選

             self.driver.find_element(By.XPATH, '//div[@class="f-fl j-chapter"]/div[@class="u-select"]/div[1]').click()

             self.driver.find_elements(By.XPATH, '//div[@class="f-fl j-chapter"]//div[@class="down f-bg j-list"]/div')[chapter_index].click()

             # 等待課程元素出現

             WebDriverWait(self.driver, 1000).until(

                 EC.presence_of_element_located(

                     (By.XPATH, '//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div'))

             )

             # 獲取章節中所有課程

             course = self.driver.find_elements(By.XPATH,'//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div/div[2]/div')

             course_names = [i.get_attribute('title') for i in course]

             for c_index in range(len(course)): # 迴圈課程

                 WebDriverWait(self.driver, 1000).until(

                     EC.presence_of_element_located(

                         (By.XPATH, '//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div'))

                 )

                 self.driver.find_element(By.XPATH,'//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div').click()

                 WebDriverWait(self.driver, 1000).until(

                     EC.presence_of_element_located(

                         (By.XPATH, '//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div/div[2]/div'))

                 )

                 # 重新獲取課程元素並點選

                 course1 = 跟單網gendan5.comself.driver.find_elements(By.XPATH, '//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div/div[2]/div')

                 course1[c_index].click()

                 time.sleep(2)

                 try:

                     try:

                         # 獲取討論元素

                         disc = self.driver.find_element(By.XPATH, '//li[@title=" 討論:討論話題 "]/span[2]')

                         if disc.is_enabled(): # 如果討論元素存在且可以點選

                             disc.click()

                             self.discuss() # 呼叫討論函式

                     except:

                         time.sleep(0.5)

                         if course_names[c_index] == ' 討論區 ':

                             self.discuss()

                 except:

                     pass

     # 討論

     def discuss(self):

         # 等待“回覆”元素出現

         WebDriverWait(self.driver, 100).until(

             EC.presence_of_element_located((By.XPATH, '//div[@class="optbar f-cb"]/a[1]/span'))

         )

         time.sleep(0.5)

         self.driver.execute_script("var q=document.documentElement.scrollTop=10000")

         time.sleep(0.5)

         try:

             content = self.driver.find_element(By.XPATH, '//div[@class="j-list"]/div/div[3]/div/div[2]/p').text

         except:

             content = self.driver.find_element(By.XPATH, '//div[@class="j-list"]/div/div[1]/div/div[2]').text

         self.driver.switch_to.frame(0)

         time.sleep(0.5)

         # 定位輸入框並輸入資訊

         self.driver.find_element(By.XPATH, '//body[@contenteditable="true"]').send_keys(content)

         self.driver.switch_to.window(self.driver.window_handles[-1])    # 進行視窗切換

         time.sleep(1)

         # 點選傳送回復

self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[4]/div[2]/div/div[1]/div/div/div[3]/div[2]/div[2]/div/div/div[5]/div/div[1]/div[2]/a[1]').click()

     def run(self):

         self.login()

         self.entering_the_course()

mooc = MOOC()

mooc.run()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2922139/,如需轉載,請註明出處,否則將追究法律責任。

相關文章