easy992
easy992

Reputation: 1

how to start autofill from the last select row in csv file python selenium webdriver after the autofill has stoped

I have created an autofill with python selenium and I have a small problem with it, the autofill stops by itself after it has reached the autofill ROW 15 or 18 or 20 and when I restart the autofill it starts from the beginning then I have the problem that I have the web form 2x-3x with the same data.

Do you perhaps have a solution so that my autofill, for example, the autofill stops at ROW 15 and I restart it, it starts again from ROW 15 so that I do not have the same data several times.

enter image description here

#-------------------------------------------------------------------------------
# Imports
import csv
from selenium import webdriver
from selenium.webdriver.common.by import By
import time


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

name = 0
age = 1
score = 2

with open('data.csv', 'r', encoding='utf8', errors='ignore') as csv_file:

    csv_reader = csv.reader(csv_file)


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

    for line in csv_reader:

        driver = webdriver.Chrome()
        driver.get('https://mysite.domain/?page_id=311')
      
        Add_to = driver.find_element(By.XPATH, "//*[@id='site-content']")
        Add_to.click()
        time.sleep(3)
        
        driver.get('https://mysite.domain/?page_id=385')
        
        name_field = driver.find_element(By.XPATH, "//*[@id='first_name']")
        name_field.send_keys(line[0])

        age_field = driver.find_element(By.XPATH, "//*[@id='last_name']")
        age_field.send_keys(line[1])

        Adress = driver.find_element(By.XPATH, "//*[@id='address_1']")
        Adress.send_keys(line[2])

        Zip = driver.find_element(By.XPATH, "//*[@id='postcode']")
        Zip.send_keys(line[3])

        City = driver.find_element(By.XPATH, "//*[@id='city']")
        City.send_keys(line[4])

        Phone = driver.find_element(By.XPATH, "//*[@id='phone']")
        Phone.send_keys(line[5])

        Email = driver.find_element(By.XPATH, "//*[@id='email']")
        Email.send_keys(line[6])

        Submit = driver.find_element(By.XPATH, "//*[@id='Submit']")
        Submit.click()
        time.sleep(3)
        


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

One of my attempts didn't really work :(

Web Automation

for line in csv_reader:
csv.reader("let lastrow = $('table').find('tr')[document.querySelectorAll('table tr').length-1];$(lastrow).find('.fa-edit').click();")

    driver = webdriver.Chrome()
    driver.get('https://mysite.domain/?page_id=311')...

Upvotes: 0

Views: 69

Answers (1)

Prodaturu
Prodaturu

Reputation: 33

I would prefer this to be a comment but I dont have enough rep for that. so... Anyways

You can achieve this by implementing a mechanism to track the last processed row and save this state externally, I chose a file to do that for me. That way, when the script is restarted, we can read the last processed row and start again from there.

Maybe try these steps.

  1. Add a function that can save the last accessed row index from the file and use it to access from where u want the next time

  2. I would suggest you to convert the CSV to list as it's much easier to work with.

  3. Make sure your loop updates from the last processed row.

Upvotes: 0

Related Questions