📌  相关文章
📜  使用Selenium和Python自动将多个响应填充到 Google 表单中(1)

📅  最后修改于: 2023-12-03 14:49:51.508000             🧑  作者: Mango

使用Selenium和Python自动将多个响应填充到 Google 表单中

在数据收集和表单填写任务中,自动化可以提高效率和减少人工操作错误。本文将介绍如何使用Selenium和Python自动填写Google表单中的多个响应,并减少手动输入的时间和错误。

准备工作
  1. 安装Selenium,可使用pip install selenium安装。

  2. 下载Chrome浏览器和Chrome浏览器驱动,在Selenium官网上进行下载,下载后解压并将驱动文件放到环境变量PATH中,以便Python能够找到它。

  3. 打开Google表单,提前手动填写好标题和表头,以提供自动化填写数据的位置。

代码实现

接下来,我们将用Python编写一段程序,在自动化填写Google表单中的多个响应数据。首先,我们需要导入所需的库和定义Google表单的URL:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

url = 'https://docs.google.com/forms/d/e/1FAIpQLSer-m4ZDsRCECdWg7M6GJwGpUu8oZAw6M6Sxceo4frD6-eL9Q/viewform'

接下来,我们需要填写表单信息。这里我们假设已经有多个响应需要填写,我们可以将响应数据存储在一个包含多个字典的列表中,每个字典表示一次响应,可以在程序中实现:

responses = [
    {'name': '张三', 'age': '25', 'gender': 'male', 'comment': '这是张三的评论'},
    {'name': '李四', 'age': '30', 'gender': 'female', 'comment': '这是李四的评论'},
    ...
]

然后,我们需要启动Chrome浏览器并访问Google表单的URL。使用Selenium中的webdriver.Chrome()方法启动Chrome浏览器:

driver = webdriver.Chrome()
driver.get(url)

接下来,我们需要设置填充表单的函数,该函数将迭代每个响应并自动填充Google表单中的所有字段。我们可以使用Selenium中的find_element_by_xpath()send_keys()方法查找和填充表单数据:

def fill_form(response):
    name_input = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div[2]/div[1]/div/div[1]/input')
    name_input.send_keys(response['name'])

    age_input = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[2]/div/div[2]/div[1]/div/div[1]/input')
    age_input.send_keys(response['age'])

    gender_select = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div[2]/div[1]/div[1]/span')
    gender_select.click()

    if response['gender'] == 'male':
        gender_option = gender_select.find_element_by_xpath('//div[@data-value="男性"]')
    elif response['gender'] == 'female':
        gender_option = gender_select.find_element_by_xpath('//div[@data-value="女性"]')
    gender_option.click()

    comment_input = driver.find_element_by_xpath('//textarea[@aria-label="请填写评论(可选)"]')
    comment_input.send_keys(response['comment'])

    submit_button = driver.find_element_by_xpath('//span[contains(text(),"提交")]')
    submit_button.click()

    time.sleep(2)

最后,我们需要迭代每个响应并调用fill_form()方法以自动填充Google表单。完成后,我们需要关闭浏览器:

for response in responses:
    fill_form(response)

driver.quit()

完整的Python代码如下:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

url = 'https://docs.google.com/forms/d/e/1FAIpQLSer-m4ZDsRCECdWg7M6GJwGpUu8oZAw6M6Sxceo4frD6-eL9Q/viewform'

responses = [
    {'name': '张三', 'age': '25', 'gender': 'male', 'comment': '这是张三的评论'},
    {'name': '李四', 'age': '30', 'gender': 'female', 'comment': '这是李四的评论'},
    ...
]


def fill_form(response):
    name_input = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div[2]/div[1]/div/div[1]/input')
    name_input.send_keys(response['name'])

    age_input = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[2]/div/div[2]/div[1]/div/div[1]/input')
    age_input.send_keys(response['age'])

    gender_select = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div[2]/div[1]/div[1]/span')
    gender_select.click()

    if response['gender'] == 'male':
        gender_option = gender_select.find_element_by_xpath('//div[@data-value="男性"]')
    elif response['gender'] == 'female':
        gender_option = gender_select.find_element_by_xpath('//div[@data-value="女性"]')
    gender_option.click()

    comment_input = driver.find_element_by_xpath('//textarea[@aria-label="请填写评论(可选)"]')
    comment_input.send_keys(response['comment'])

    submit_button = driver.find_element_by_xpath('//span[contains(text(),"提交")]')
    submit_button.click()

    time.sleep(2)

driver = webdriver.Chrome()
driver.get(url)

for response in responses:
    fill_form(response)

driver.quit()

以上就是如何使用Selenium和Python自动将多个响应填充到Google表单中的完整指南。