Automating Login and Data Extraction from a GPS Web System with Selenium
This guide demonstrates how to use Python's Selenium library to log into a GPS web system, locate elements via name and XPath, handle iframes, click list items, and extract dynamic data, including detailed installation steps for Selenium and ChromeDriver.
This article provides a step‑by‑step tutorial for automating the login process and subsequent data extraction from a GPS‑type web application using Python's Selenium library.
First, install Selenium via pip install selenium and download the matching ChromeDriver version for your Chrome browser, placing the chromedriver.exe in the same directory as your Python executable.
Open the target login page, use the browser's developer tools to inspect the username and password fields, and note their name attributes for locating them in code.
The core automation script is shown below. It creates a Chrome WebDriver instance, navigates to the page, fills in the credentials, clicks the login button, switches to the appropriate iframe, and finally clicks a specific row in a table using an XPath expression.
from selenium import webdriver
import time
opt = webdriver.ChromeOptions() # create options
# opt.set_headless() # optional headless mode
driver = webdriver.Chrome(options=opt) # create browser object
driver.get('网页') # open target URL
time.sleep(1) # wait for page load
# Fill username
elem = driver.find_element_by_name("username")
elem.clear()
elem.send_keys("账号")
time.sleep(1)
# Fill password
elem = driver.find_element_by_name("password")
elem.clear()
elem.send_keys("密码")
time.sleep(1)
# Click login button via XPath
driver.find_element_by_xpath("/html/body/div/div[2]/div/form/div/input").click()
time.sleep(2)
# Switch to iframe to access inner elements
driver.switch_to.frame("mainFrame")
time.sleep(2)
# Click the desired table row using XPath
driver.find_element_by_xpath("//table[@id='contentTable']//tr[2]/td[1]").click()After logging in, the script switches to the mainFrame iframe because the list elements reside there. It then uses a full XPath (e.g., //table[@id='contentTable']//tr[2]/td[1] ) to locate and click a specific entry in the table.
Finally, a time.sleep(2) pause is suggested to allow the request to return data. At this point you can add additional code to read the element’s content, extract the GPS coordinates, and write them to a file for further analysis.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.