Home > Net >  Fill web form with CSV
Fill web form with CSV

Time:12-06

So I need to take a CSV from one platform and for another platform fill a form and submit the data. With Python and Selenium I've got this working:

#-------------------------------------------------------------------------------
# Imports
import csv
import requests
from selenium import webdriver
import time

#-------------------------------------------------------------------------------
# Setup

# name = 0
# age = 1
# score = 2

with open('data.csv', 'r') as csv_file:

    csv_reader = csv.reader(csv_file)

#-------------------------------------------------------------------------------
# Web Automation

    for line in csv_reader:

        driver = webdriver.Chrome(executable_path='./chromedriver')
        driver.get('http://site')

        time.sleep(3)
        title_field = driver.find_element_by_xpath('//*[@id="TITLE"]')
        title_field.send_keys(line[0])

        fname_field = driver.find_element_by_xpath('//*[@id="FIRSTNAME"]')
        fname_field.send_keys(line[1])

        lname_field = driver.find_element_by_xpath('//*[@id="LASTNAME"]')
        lname_field.send_keys(line[2])
        
        phone_field = driver.find_element_by_xpath('/html/body/div/div/div[5]/form/div[6]/div/div/div/div/div[1]/div[2]/input[2]')
        phone_field.send_keys(line[3])

        mail_field = driver.find_element_by_xpath('//*[@id="EMAIL"]')
        mail_field.send_keys(line[4])

        submit = driver.find_element_by_xpath('//*[@id="s-form"]/div[9]/div/button')
        submit.click()

#-------------------------------------------------------------------------------

However there are pitfalls: Firstly this needs to be given to a client, so thought of making the Python script into an app however that won't work due to Selenium's consistent changes. To roll this out to the client updates, unless this is the last option then its alright.

This opens a new window that takes up space, how do I make it so its only one time load and fill , submit, success, fill, submit, success etc.

Or does anyone know of any better way ? I was hoping for a situation could upload the csv and it could fill in the files on the server however the form is from a service.

Any and all help appreciated.

CodePudding user response:

These points might help-

  1. Use the webdriver-manager package. This will let you work on the latest version of chromedriver without even having it to install and specify the path to the driver each time. This package can be added to the requirements.txt file, in case you're planning to run this file from a server.

  2. You're initializing the driver inside the for loop, causing it to open a new window each time in the loop.

  3. Also try using headless mode, which is considered somewhat faster.

Sample snippet-

import csv
import requests
from selenium import webdriver
import time
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options

with open('data.csv', 'r') as csv_file:

    csv_reader = csv.reader(csv_file)
    
    options = Options()
    options.headless = True
    driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)

    for line in csv_reader:
        driver.get('http://site')

        time.sleep(3)
        title_field = driver.find_element_by_xpath('//*[@id="TITLE"]')
        title_field.send_keys(line[0])

        fname_field = driver.find_element_by_xpath('//*[@id="FIRSTNAME"]')
        fname_field.send_keys(line[1])

        lname_field = driver.find_element_by_xpath('//*[@id="LASTNAME"]')
        lname_field.send_keys(line[2])
        
        phone_field = driver.find_element_by_xpath('/html/body/div/div/div[5]/form/div[6]/div/div/div/div/div[1]/div[2]/input[2]')
        phone_field.send_keys(line[3])

        mail_field = driver.find_element_by_xpath('//*[@id="EMAIL"]')
        mail_field.send_keys(line[4])

        submit = driver.find_element_by_xpath('//*[@id="s-form"]/div[9]/div/button')
        submit.click()
  • Related