SoFunction
Updated on 2024-10-30

Python selenium simulates manual operation to realize unattended points swiping function

Often worried about the school's various brush marks, learned that there is no hope for the beginning of the school year, and later to brush the class, so automated for once, learning without using it isuneducated witty

Four Modules

initialization

from selenium import webdriver

if __name__ == '__main__':
	driver = ()
	url = '/points/?ref=/points/'
	(url = url)

Points for effective article reading + Points for article length

def article():
	(url='/d05cad69216e688d304bb91ef3aac4c6/')
	# This site is only one of the many categories of articles to read, there are many more categories of articles to read, just change the link!
	
  article_lis = WebDriverWait(driver,100).until(EC.presence_of_all_elements_located((,'//span[@style=white-space: nowrap;]')))
  # Show wait WebDriverWait(driver,100).until() Wait here until the condition is met or the wait time exceeds 100, i.e. use xpath to find a span node that matches the attribute style = "white-space: nowrap;";
  
  print('Total %s articles found' % len(article_lis))
  article_num = 0
  # article_num The maximum number of articles that can be effectively read per day is 6, but to ensure that the effective length of time is 12 minutes, 2 more articles are added.
  
  for data in article_lis: # Iterate over the list of found articles for simulated reading
    if article_num >= 8: # Jump out of the loop when enough 8 articles have been read, ending the article swipe
      break
    try:
      loading_page(data)
      print('Loaded', )
      # Output brushed articles to derive progress
      article_num += 1
    except:
      continue
      
def loading_page(element = None):
  ()
  ele = driver.find_element_by_xpath('//a[contains(class,"search-icon")]')
  ele.send_keys(Keys.PAGE_DOWN)
  # Simulate page scrolling. The method used is to find the 'Search' function button and, instead of clicking it, simulate a click on the button PAGE_DOWN
 
  (120)
  # Stay on each page for two minutes, at least 30 seconds. After testing, a post request will be submitted every 30s, and only after the request, the valid read count will be +1.
  return None

 

Points for effective video viewing + Points for video duration

 (url = '/4426aa87b0b64ac671c96379a3a8bd26/#11c4o0tv7nb-5')
  # Ibid, this site is just one of the many categories of videos to watch, there are many more categories of videos to watch, just change the link
  
  video_lis = WebDriverWait(driver,100).until(EC.presence_of_all_elements_located((,'//div[@style="margin: 0px auto;"]/div/div')))
  # Ibid, show waiting
  
  print('Total %s articles found' % len(video_lis))
  video_long = 0
  # Record the time that has been played
  
  video_lis_1 = []
  for data in video_lis:
    video_lis_1.append(data.get_attribute('data-link-target'))
	# Unlike articles, where reading an article is a simulated click, video playback gets the URL and puts it into video_lis_1
  
  for url_1 in video_lis_1: # Iterate through the video list and play the video
    if video_long >= 1080: # The video is 18 minutes long, i.e. 1080 seconds, and ends playback after more than 18 minutes of playback time, which is actually judged based on minutes as well, and I don't know why the seconds were used at that point
      break
    try:
      (url = url_1)
      tim_now = loading_video(driver = driver)
      video_long += tim_now*60
      # loading_video returns the number of minutes read, so *60
      print('Video playing, played for %s of a second'%video_num)
    except:
      continue
      
def loading_video(driver = None):
  elem_first = WebDriverWait(driver, 100).until(EC.presence_of_all_elements_located((, '//span[@class="duration"]')))
  elem_start = driver.find_element_by_xpath('//div[@class="outter"]')
  elem_start.click()
  # No autoplay after opening a web page, presumably because it's a direct open URL
  # Because before this I also directly through the simulation click to open the page, the result is autoplay, but there is a little bit does not meet my needs at that time, so change to open the web page way
  
  tim_num = (int(elem_first[0].text[0])*10 + int(elem_first[0].text[1]))
	# The goal is to get the total time of the video, taking only the minutes
  if tim_num != 0: # Because some videos it's less than a minute, so add a judgmental condition #
    (tim_num * 60)
    return tim_num
  else:
    (60)
    # It's less than a minute, it's less than a minute without playing it #
    return 1

 

Daily Answer Points

def DaTi():
	(url = '/points/')
  elem_juje = WebDriverWait(driver,100).until(EC.presence_of_element_located((,'//div[@class="q-header"]')))
  juje = elem_juje.get_attribute('innerText')
  # In Daily Answer, there are three categories of questions, judgmental, multiple choice, and fill-in-the-blank, so first get the category of the question
  # Note Note in particular that and xpath text to get slightly different, get the text way for .get_attribute('innerText')
  
  (1) # Wait a second. It doesn't really matter, but it's better to wait a second to prevent your computer from lagging from operating too quickly.
  if 'Choice' in juje:
    elem_tishi = WebDriverWait(driver, 100).until(EC.presence_of_element_located((, '//span[@class="tips"]')))
    elem_tishi.click()
    # In the quiz, the answer is marked in red in the view prompt, so first simulate clicking on the view answer to load the answer
    (0.5)
    elem_answer = WebDriverWait(driver, 100).until(EC.presence_of_all_elements_located((, '//div[@class="line-feed"]/font')))
    # Extract the text marked in red, i.e. the answer
    (0.5)
    # And there's no real point
    
    ans_lis = []
    for elem in elem_answer:
      ans_lis.append(elem.get_attribute('innerText'))
    # Because multiple choice questions, not necessarily single choice, are stored to make it easier to find the answer among the options #
    print('Getting answers')
    (0.5)
    # And there's no real point
    
    elem_juje.click()
		# The reason for simulating another click is that the view hints box is still open, and if it is not closed, it will affect the submission of the answer.
		# Simulate clicking on a web page to close the View Alerts box
    (0.5)
    
    elem_xuanxiang = WebDriverWait(driver, 100).until(EC.presence_of_all_elements_located((, '//div[@class="question"]/div[@class="q-answers"]/div[contains(@class,"q-answer")]')))
    # Get all option nodes
    
    for elem in elem_xuanxiang:
      data = elem.get_attribute('innerText')[3:].replace('-','')
      print(data)
      for i in ans_lis:
        if i in data:
          ()
          ans_lis.remove(i) 
          # Traverse through the answers in order A -- > D, checking them against each other so that the answer is removed when the requirements are met.
          # Prevent multiple clicks on options from causing deselections or other errors
          (0.5) # Prevent too fast operation by waiting 0.5 seconds after each selection
          break
          
    elem_next = WebDriverWait(driver, 100).until(EC.presence_of_element_located((,'//div[@class="action-row"]/button')))
    elem_next.click()
    # Simulate clicking the OK button to jump to the next question
    DaTi(driver)
  elif 'Fill in' in juje:
  
    elem_tishi = WebDriverWait(driver,100).until(EC.presence_of_element_located((,'//span[@class="tips"]')))
    elem_tishi.click()
    (0.5)
    elem_answer = WebDriverWait(driver,100).until(EC.presence_of_element_located((,'//div[@class="line-feed"]')))
    (0.5)
    if 'Please watch the video' in elem_answer.get_attribute('innerText'):
      input('No need to click OK after manually selecting an answer, type enter here to continue')
      elem_next = WebDriverWait(driver, 100).until(EC.presence_of_element_located((, '//div[@class="action-row"]/button')))
      elem_next.click()
    # There will be questions about watching the video in the fill-in-the-blanks questions and the viewing prompt will say 'please watch the video' instead of giving the answer directly, so choose it manually
    elem_answer = WebDriverWait(driver,100).until(EC.presence_of_all_elements_located((,'//div[@class="line-feed"]/font')))
    answer = []
    (0.5)
    for elem in elem_answer:
      (elem.get_attribute('innerText'))
		# Also fill in the blanks with multiple blanks, so put the answers in the list
    (1)
    elem_data = WebDriverWait(driver,100).until(EC.presence_of_all_elements_located((,'//input[@class="blank"]')))
    # Find every empty space
    for i in range(len(answer)):
      elem_data[i].send_keys(answer[i])
      (0.5)
    # Write the corresponding answer by traversing the empty
    elem_data[0].click()
    (1)
    # Simulate clicking on a web page, because not clicking on a web page after writing null will have the OK button as unclickable
    elem_next = WebDriverWait(driver,100).until(EC.presence_of_element_located((,'//div[@class="action-row"]/button')))
    elem_next.click()
    DaTi(driver)
  elif 'Judgment' in juje:# Ditto, judgmental questions don't give direct answers, so you have to do it manually I guess
    input('No need to click OK after manually selecting an answer, type enter here to continue')
    elem_next = WebDriverWait(driver, 100).until(EC.presence_of_element_located((, '//div[@class="action-row"]/button')))
    elem_next.click()
    DaTi(driver)

 

Combined with PyQt5, the final result is shown

Disclaimer: The picture has no other meaning, the personal feeling is very joyful, the picture is transferred from ------ Baidu pictures

图片没有别的意思

summarize

This article on Python selenium simulation of manual operation to achieve unattended brush points function is introduced to this article, more related to Python selenium brush points content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!