To automate the process described using Selenium for web scraping, you'll need to properly simulate the sequence of actions: logging in, navigating to the "Renewal of Patent" section, entering the patent number, submitting it, and then capturing the required information.
General Approach
- Log in with your credentials.
- Navigate to the "Renewal of Patent" button and click it.
- Enter the patent number and submit the form.
- Enter any character in the details of the request.
- Scrape the first line containing the renewal information.
- Save the captured information into a CSV file.
Example Code
Here is an example implementation based on your description. Make sure to adjust the element locators according to the actual HTML structure if needed:
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def scrape_patent_renewals(excel_file_path, output_csv):
# Read the patent numbers from the Excel file
df = pd.read_excel(excel_file_path)
patent_numbers = df['Patent No'].tolist() # Adjust column name as necessary
# Initialize the Chrome driver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
# Navigate to the login page
driver.get("https://ipronline.ipindia.gov.in/epatentfiling/goForLogin/doLogin")
# Wait for the login elements to load and enter credentials
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "username"))).send_keys("your_username")
driver.find_element(By.NAME, "password").send_keys("your_password")
driver.find_element(By.NAME, "loginButton").click() # Adjust button name if necessary
# Wait for the page to load and ensure the user is logged in
time.sleep(2)
# Click on the "Renewal of Patent" button
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "ctl00_ctl00_ContentPlaceHolder1_FilingPane_content_LinkRenewalOfPatent"))).click()
# Prepare data for CSV
renewal_data = []
# Loop through each patent number
for patent_no in patent_numbers:
# Enter the patent number
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "patentNumber"))).send_keys(patent_no)
# Click on submit/patent number action (you might need to adjust this locator)
driver.find_element(By.NAME, "submit").click() # Adjust button name if necessary
time.sleep(2) # Wait for the response page to load
# Enter any character in details of request
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "reviewInput"))).send_keys("A")
driver.find_element(By.NAME, "submitReview").click() # Adjust button name if necessary
time.sleep(2) # Wait for renewal info to load
# Capture the first line in the renewal information
try:
renewal_info_element = WebDriverWait(driver, 10).until(EC.presence_of_element_located(
(By.XPATH, "//div[contains(text(), 'Renewal against patent number:')]")
))
renewal_info = renewal_info_element.text
renewal_data.append({
"Patent No": patent_no,
"Renewal of Patents": renewal_info
})
# Go back to the patent input page, if needed
driver.back()
time.sleep(2)
except Exception as e:
print(f"Error processing patent number {patent_no}: {str(e)}")
driver.back()
time.sleep(2)
# Save the data into a CSV file
result_df = pd.DataFrame(renewal_data)
result_df.to_csv(output_csv, index=False)
# Close the browser
driver.quit()
# Main execution
# Make sure to replace "path_to_your_excel_file.xlsx" and "output_file.csv"
scrape_patent_renewals("path_to_your_excel_file.xlsx", "patent_renewals.csv")
Key Adjustments:
- Web Element Locators: Adjust the locators (e.g.,
By.NAME, By.ID, By.XPATH) according to the actual structure of the HTML you are working with. You may need to inspect elements for any changes.
- Excel File Column Name: Ensure to match the column name in the Excel file containing the patent numbers with the one used in the script (
'Patent No').
- Error Handling: If any element is not found, the script catches exceptions, allowing you to debug or skip problematic patent numbers.
Notes:
- Privacy & Terms of Use: Ensure that your web scraping activities comply with the website's terms of use and privacy policies.
- Waiting for Elements: Using
WebDriverWait is preferred over fixed sleep time, as it enhances the script's robustness in handling page load times.
- Headless Mode (Optional): If you want to run the browser in headless mode (invisible), add options to
Chrome() to run without a GUI.