python自动填问卷

  • 本代码解决了智能验证和滑块验证,但是貌似问卷星有什么机制,一次只能刷20份左右。多了,滑块验证就会失败。然后隔一会,就又可以刷了,大约隔几十分钟。

  • 添加了个selenium伪装,使得检测到不是自动控制的程序。

  • 鼠标拖动操作,只需创建对象,然后传入相应的参数即可。

  • 关于选择题的定位,xpath路径有规律。

代码

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
import pyautogui


def run(num, url):
    for i in range(num):
        option = webdriver.ChromeOptions()
        option.add_experimental_option('excludeSwitches', ['enable-automation'])
        option.add_experimental_option('useAutomationExtension', False)

        browser = webdriver.Chrome(options=option)

        browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
                                {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
        browser.maximize_window()  # 窗口最大化

        browser.get(url)
        time.sleep(2)

        if browser.find_element(by=By.XPATH, value="//div[@for='q1_2']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q1_2']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q2_1']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q2_1']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q3_3']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q3_3']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q4_1']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q4_1']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q5_2']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q5_2']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q6_2']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q6_2']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q7_1']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q7_1']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q8_2']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q8_2']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q9_2']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q9_2']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q10_3']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q10_3']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q11_3']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q11_3']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q12_2']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q12_2']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q13_1']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q13_1']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q14_2']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q14_2']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q15_1']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q15_1']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q16_4']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q16_4']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q17_2']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q17_2']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q18_2']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q18_2']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q19_2']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q19_2']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@for='q20_1']"):
            browser.find_element(by=By.XPATH, value="//div[@for='q20_1']").click()
        time.sleep(1)

        if browser.find_element(by=By.XPATH, value="//div[@class='submitbtn mainBgColor']"):
            browser.find_element(by=By.XPATH, value="//div[@class='submitbtn mainBgColor']").click()

        time.sleep(1)
        try:
            element = browser.find_element_by_css_selector("#alert_box > div:nth-child(2) > div:nth-child(2) > button")
            element.click()
            time.sleep(1)
            yanz = browser.find_element_by_id("rectMask")
            yanz.click()
            time.sleep(4)
        except:
            pass

        # 滑块验证

        pyautogui.moveTo(789, 805, duration=1)  # 传入屏幕的位置
        time.sleep(0.5)
        pyautogui.dragRel(370, 0, duration=0.8)  # 拖动

        # pyautogui.moveTo(789, 805, duration=1)   # 回到原处
        # pyautogui.moveRel(370, 0, duration=1)

        # pyautogui.dragRel(256, 0, duration=0.8)
        # pyautogui.moveTo(789, 805)
        # time.sleep(0.5)
        # pyautogui.dragRel(370, 0, duration=0.8)

        time.sleep(5)
        browser.close()
        print("第{}份已经完成".format(i))



if __name__ == '__main__':
    num = 100  # 填的份数
    url = 'https://www.wjx.cn/vm/exFwRho.aspx'  # 链接
    run(num, url)

你可能感兴趣的:(爬虫,python,chrome,单元测试)