Bronson77
Bronson77

Reputation: 299

Iterate through CSV rows with Pandas, Perform Selenium Action

I have a CSV file that was created using Pandas. Below is the output from the following code:

   test = pd.read_csv('order.csv', header=0)
   print(test.head())

      3  16258878505032
   0  3  16258876670024
   1  3  16258876899400
   2  3  16258876997704

The only data I need to be processed is the information in the 2nd column and the information on the 3rd column. This is purchase order data where the 2nd column represents a "quantity" and the 3rd column represents the "sku".

I need to take row 1, col 2 and inject it into an input field using selenium. I need row 1, col 3 and perform an action of selecting a sku on a webpage. Add the item to a cart and loop back through process row 2, row 3 etc.

I know how to write the selenium code to perform the web based actions, but not sure how to write the pandas/python code to iterate through the CSV file one row at a time and how to call those values out. My logic would be the following.

read order.csv
    get quantity value and sku value for row (one row at the time)
        visit website, inject quantity value
        remain on website, select sku
        add to cart

        repeat loop until no more rows to process

Thanks for your help.

Upvotes: 5

Views: 25451

Answers (1)

jezrael
jezrael

Reputation: 863166

First use parameter names in read_csv for avoid convert first row of data to columns names:

test = pd.read_csv('order.csv', names=['quantity','sku'])
print (test)
   quantity             sku
0         3  16258878505032
1         3  16258876670024
2         3  16258876899400
3         3  16258876997704

Because working with selenium and web is possible use DataFrame.iterrows or another loops solutions:

def func(x):
    q = x['quantity']
    sku = x['sku']
    print (q, sku)
    #add selenium code

df.apply(func, axis=1)

Or:

for i, row in test.iterrows():
    q = row['quantity']
    sku = row['sku']
    print (q, sku)
    #add selenium code

Upvotes: 9

Related Questions