cmrussell
cmrussell

Reputation: 2112

Use a csv to fill out a web form with python

I am trying to send several items from a CSV file to a webform using python so I don't have to type it all in by hand, especially when I update the sheet later. I tried using the answer to this question and the page comes up and seems to "submit" but I get told the import failed.

My Code

from bs4 import BeautifulSoup
from requests import get
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import pandas as pd

# using Pandas to read the csv file
source_information = pd.read_csv('C:/chrome_driver/test_csv.csv', header=None, skiprows=[0])
print(source_information)

# setting the URL for BeautifulSoup to operate in
url = "https://www.roboform.com/filling-test-all-fields"
my_web_form = get(url).content
soup = BeautifulSoup(my_web_form, 'html.parser')


# creating a procedure to fill the form
def fulfill_form(first, email):
    # Setting parameters for selenium to work
    path = r'C:/chrome_driver/chromedriver.exe'
    options = webdriver.ChromeOptions()
    options.add_argument("--start-maximized")
    driver = webdriver.Chrome(path, options=options)
    driver.get(url)

    # use Chrome Dev Tools to find the names or IDs for the fields in the form
    input_first = driver.find_element_by_name('02frstname')
    input_email = driver.find_element_by_name('24emailadr')
    submit = driver.find_element_by_name('Reset')

    # input the values and hold a bit for the next action
    input_first.send_keys(first)
    time.sleep(1)
    input_email.send_keys(email)
    time.sleep(5)
    submit.click()
    time.sleep(7)


# creating a list to hold any entries should them result in error
failed_attempts = []

# creating a loop to do the procedure and append failed cases to the list
for customer in source_information:
    try:
        fulfill_form(str(source_information[0]), str(source_information[1]))
    except:
        failed_attempts.append(source_information[0])
        pass

if len(failed_attempts) > 0:
    print("{} cases have failed".format(len(failed_attempts)))

print("Procedure concluded")

This tells me that "2 cases have failed" I checked the output of my "source_information" and it shows the following

        0                 1
0   Corey    [email protected]
1  Breana  [email protected]

Where am I going wrong?

Upvotes: 0

Views: 1070

Answers (1)

MDR
MDR

Reputation: 2670

Maybe:

submit = driver.find_element_by_name('Reset')

Should be...

submit = driver.find_element_by_xpath("//input[@type='reset' and @value='Reset']")

Based on the page source of (it doesn't have a name)...

<input type="reset" value="Reset">

...and note the type reset vs the value Reset.

enter image description here


Then you have source_information as a dataframe so you probably want to change...

# creating a loop to do the procedure and append failed cases to the list
for customer in source_information:
    try:
        fulfill_form(str(source_information[0]), str(source_information[1]))
    except:
        failed_attempts.append(source_information[0])
        pass

To something like...

# creating a loop to do the procedure and append failed cases to the list
for customer in source_information.iterrows():
    try:
        fulfill_form(customer[1][0], customer[1][1])
    except:
        failed_attempts.append(source_information[1][0])
        pass

I'd also suggest changing all your time.sleep(5) and time.sleep(7) to 1 or 2 so it runs a little quicker.

Obviously this is all from looking at the code without running your data and seeing what happens.


Additional:

I reread the question and you do have an example of test data from the failures. Running this for the changes shown above works.

enter image description here

Upvotes: 1

Related Questions