import time
import logging
import tkinter as tk
from tkinter import messagebox
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
# --- Logging Setup ---
logging.basicConfig(
filename='form_fill.log',
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
# --- Excel File ---
df = pd.read_excel("your_excel_file.xlsx")
# --- Initialize WebDriver ---
driver = webdriver.Chrome()
driver.get("https://popcensus68.nso.go.th")
wait = WebDriverWait(driver, 20)
# --- GUI alert function ---
def popup_and_wait(message, timeout=60):
root = tk.Tk()
root.withdraw()
root.after(timeout * 1000, root.destroy)
messagebox.showinfo("ขั้นตอนถัดไป", message)
root.mainloop()
# --- Main Form Loop ---
for index, row in df.iterrows():
try:
# จังหวัด
wait.until(EC.presence_of_element_located((By.NAME, 'selCwt')))
Select(driver.find_element(By.NAME, 'selCwt')).select_by_visible_text(row["จังหวัด"].strip())
# ยืนยันหน้า
popup_and_wait(f"✅ แถว {index+1}: {row['จังหวัด']} - โปรดยืนยันบนเว็บ")
# อำเภอ
wait.until(EC.presence_of_element_located((By.NAME, 'selAmp')))
Select(driver.find_element(By.NAME, 'selAmp')).select_by_visible_text(row["อำเภอ"].strip())
# ตำบล
wait.until(EC.presence_of_element_located((By.NAME, 'selTam')))
Select(driver.find_element(By.NAME, 'selTam')).select_by_visible_text(row["ตำบล"].strip())
# หมู่ที่
wait.until(EC.presence_of_element_located((By.NAME, 'selmoo')))
Select(driver.find_element(By.NAME, 'selmoo')).select_by_visible_text(str(row["หมู่ที่"]).strip())
# บ้านเลขที่
wait.until(EC.presence_of_element_located((By.ID, 'noHouseSearch')))
house_input = driver.find_element(By.ID, 'noHouseSearch')
house_input.clear()
house_input.send_keys(str(row["บ้านเลขที่"]).strip())
popup_and_wait("➡️ ตรวจสอบบ้านเลขที่ แล้วคลิกถัดไปในเว็บ")
# ชื่อหมู่บ้าน
try:
vil_input = driver.find_element(By.ID, 'VilName')
driver.execute_script("arguments[0].removeAttribute('disabled')", vil_input)
vil_input.clear()
vil_input.send_keys(str(row["ชื่อหมู่บ้าน"]).strip())
except Exception as e:
logging.warning(f"⚠️ ไม่สามารถกรอกชื่อหมู่บ้าน: {e}")
# ประเภทบ้าน
wait.until(EC.presence_of_element_located((By.ID, 'BuildingType')))
Select(driver.find_element(By.ID, 'BuildingType')).select_by_visible_text(row["ประเภทบ้าน"].strip())
# วัสดุก่อสร้าง
mat_text = row["วัสดุก่อสร้าง"].strip()
Select(driver.find_element(By.ID, 'ConstructionMaterial')).select_by_visible_text(mat_text)
if "อื่น" in mat_text:
driver.find_element(By.ID, 'ConstructionMaterialOther').send_keys(str(row["วัสดุก่อสร้างอื่นๆ"]).strip())
# การครอบครองบ้าน
res_text = row["ครอบครองบ้าน"].strip()
Select(driver.find_element(By.ID, 'TenureResidence')).select_by_visible_text(res_text)
if "อื่น" in res_text:
driver.find_element(By.ID, 'TenureResidenceOther').send_keys(str(row["ครอบครองบ้านอื่นๆ"]).strip())
# การครอบครองที่ดิน
land_text = row["ครอบครองที่ดิน"].strip()
Select(driver.find_element(By.ID, 'TenureLand')).select_by_visible_text(land_text)
if "อื่น" in land_text:
driver.find_element(By.ID, 'TenureLandOther').send_keys(str(row["ครอบครองที่ดินอื่นๆ"]).strip())
# จำนวนสมาชิก
Select(driver.find_element(By.ID, 'NumberOfHousueholdMember')).select_by_visible_text(str(int(row["จำนวนสมาชิก"])))
# สมาชิกคนที่ 1
Select(driver.find_element(By.ID, "Title-1")).select_by_visible_text(row["คำนำหน้า"])
if "อื่น" in row["คำนำหน้า"]:
driver.find_element(By.ID, "TitleOther-1").send_keys(str(row["คำนำหน้าอื่นๆ"]))
driver.find_element(By.ID, "FirstName-1").send_keys(str(row["ชื่อ"]))
driver.find_element(By.ID, "LastName-1").send_keys(str(row["นามสกุล"]))
Select(driver.find_element(By.ID, "Relationship-1")).select_by_visible_text(str(row["ความสัมพันธ์"]))
Select(driver.find_element(By.ID, "Sex-1")).select_by_visible_text(str(row["เพศ"]))
Select(driver.find_element(By.ID, "MonthOfBirth-1")).select_by_visible_text(str(row["เดือนเกิด"]))
driver.find_element(By.ID, "YearOfBirth-1").send_keys(str(int(row["ปีเกิด"])))
if pd.notna(row["อายุ"]):
driver.find_element(By.ID, "Age_01-1").clear()
driver.find_element(By.ID, "Age_01-1").send_keys(str(int(row["อายุ"])))
logging.info(f"✅ กรอกแถวที่ {index + 1} เรียบร้อย")
popup_and_wait("🎯 ตรวจสอบความถูกต้อง แล้วคลิก 'บันทึก' บนเว็บไซต์")
except Exception as e:
logging.error(f"❌ แถวที่ {index + 1} ล้มเหลว: {e}")
continue
# --- ปิดเบราว์เซอร์เมื่อผู้ใช้ยืนยัน ---
if input("พิมพ์ 'exit' เพื่อปิดเบราว์เซอร์: ").strip().lower() == 'exit':
driver.quit()
logging.info("เบราว์เซอร์ถูกปิดเรียบร้อยแล้ว")
Python Script ที่ใช้ Selenium
✅ เปิด Browser
✅ อ่าน Excel
✅ กรอกจังหวัด → รอผู้ใช้กดยืนยัน
✅ กรอกข้อมูลสมาชิก (ชื่อ-นามสกุล ฯลฯ)
✅ กดปุ่มบันทึก
✅ ปิด browser เมื่อเสร็จ
- โค้ดนี้เป็น Python Script ที่ใช้ Selenium เพื่อ:
✅ อ่านข้อมูลจังหวัดจาก Excel แล้วกรอกอัตโนมัติลงในเว็บไซต์
✅ หลังจากกรอกข้อมูล จะรอให้คุณกดปุ่ม “ยืนยัน” ด้วยตนเองบนเว็บ และเมื่อกดEnter
ในคอนโซล → ไปกรอกแถวถัดไป
ข้อกำหนดก่อนใช้งาน
- ติดตั้งไลบรารี:
bashCopyEditpip install selenium pandas openpyxl
สรุปฟังก์ชันการทำงาน
ลำดับ | ทำอะไร |
---|---|
1 | เปิด Excel และโหลดชื่อจังหวัดทั้งหมด |
2 | เปิดเว็บ https://popcensus68.nso.go.th |
3 | เลือกจังหวัดใน dropdown |
4 | รอให้ผู้ใช้กด “ยืนยัน” เองบนเว็บ |
5 | กด Enter เพื่อไปแถวถัดไป |
6 | เมื่อครบแล้ว → ปิดเว็บ |