'how to loop through all the entries in excel sheet in selenium python
So I am using selenium for my web automation. I have a excel sheet with my data entries. Right now I am able to fill my excel entries but its filling only first entry after that my program just stops. I have more than 1,50,000 entries. I want my program to keep running until it fills all the entries in my form. How do I do that. Please help me !
My Entries in excel
My code
from sre_parse import State
from tkinter.tix import Select
from unicodedata import name
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import pandas as pd
import xlrd
import time
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), chrome_options=chrome_options)
driver.get("https://ssg2021.in/citizenfeedback")
# readign the excel file
df = pd.read_excel('Rajnandgaon_List2.xlsx')
# looping through all the data
for i in df.index:
time.sleep(3)
# selcting states
state_select = driver.find_element(By.XPATH,'//*[@id="State"]')
drp1 = Select(state_select)
drp1.select_by_visible_text('Chhattisgarh')
time.sleep(2)
# selecting district
district_select = driver.find_element(By.XPATH,'//*[@id="District"]')
drp2 = Select(district_select)
drp2.select_by_visible_text('RAJNANDGAON')
entry = df.loc[i]
# entering the age
age = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[4]/div[1]/div/div/input')
age.send_keys(str(entry['age']))
# respondant name
rs_name = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[4]/div[2]/div/div[1]/div/input')
rs_name.send_keys(entry['name'])
# rs mobile number
rs_number = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[4]/div[2]/div/div[2]/div/input')
rs_number.send_keys(str(entry['mobile number']))
# rs gender
gender = driver.find_element(By.XPATH,'//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[4]/div[2]/div/div[3]/div/select')
rs_gender = Select(gender)
rs_gender.select_by_visible_text('Male')
# submitting the form
submit = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[5]/input')
submit.click()
# second page
# radio button 1
radio_1 = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[1]/div[2]/div[1]/label[1]/input')
radio_1.click()
# radio button 2
radio_1 = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[2]/div[2]/div[1]/label[1]')
radio_1.click()
# radio button 3
radio_1 = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[3]/div[2]/div[1]/label[1]')
radio_1.click()
# radio button 4
radio_1 = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[4]/div[2]/div[1]/label[1]')
radio_1.click()
# radio button 5
radio_1 = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[5]/div[2]/div[1]/label[1]')
radio_1.click()
submit2 = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[2]/input')
submit2.click()
Solution 1:[1]
Your problem can be solve using below code:
from sre_parse import State
from tkinter.tix import Select
from unicodedata import name
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import pandas as pd
import xlrd
import time
import openpyxl
# readign the excel file
ExcelPath = 'Excel_Path'
df = pd.read_excel(ExcelPath)
wb = openpyxl.load_workbook(ExcelPath)
ws = wb.worksheets[0]
maxrow = ws.max_row
# looping through all the data
for i in range(2, maxrow + 1):
# Every Time Browser will open
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome()
driver.get("https://ssg2021.in/citizenfeedback")
time.sleep(3)
# selcting states
state_select = driver.find_element(By.XPATH, '//*[@id="State"]')
drp1 = Select(state_select)
drp1.select_by_visible_text('Chhattisgarh')
time.sleep(2)
# selecting district
district_select = driver.find_element(By.XPATH, '//*[@id="District"]')
drp2 = Select(district_select)
drp2.select_by_visible_text('RAJNANDGAON')
entry = df.loc[i]
# entering the age
age = driver.find_element(
By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[4]/div[1]/div/div/input')
age.send_keys(str(entry['age']))
# respondant name
rs_name = driver.find_element(
By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[4]/div[2]/div/div[1]/div/input')
rs_name.send_keys(entry['name'])
# rs mobile number
rs_number = driver.find_element(
By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[4]/div[2]/div/div[2]/div/input')
rs_number.send_keys(str(entry['mobile number']))
# rs gender
gender = driver.find_element(
By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[4]/div[2]/div/div[3]/div/select')
rs_gender = Select(gender)
rs_gender.select_by_visible_text('Male')
# submitting the form
submit = driver.find_element(
By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[5]/input')
submit.click()
# second page
# radio button 1
radio_1 = driver.find_element(
By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[1]/div[2]/div[1]/label[1]/input')
radio_1.click()
# radio button 2
radio_1 = driver.find_element(
By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[2]/div[2]/div[1]/label[1]')
radio_1.click()
# radio button 3
radio_1 = driver.find_element(
By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[3]/div[2]/div[1]/label[1]')
radio_1.click()
# radio button 4
radio_1 = driver.find_element(
By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[4]/div[2]/div[1]/label[1]')
radio_1.click()
# radio button 5
radio_1 = driver.find_element(
By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[5]/div[2]/div[1]/label[1]')
radio_1.click()
submit2 = driver.find_element(
By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[2]/input')
submit2.click()
# After Clicking Submit Button Browser Will Quit
driver.quit()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Devam Sanghvi |

