This is the code I have created using selenium to scrape data from the webpage- ‘https://tmrsearch.ipindia.gov.in/eregister/’. This code is used to get the coordinates of an element having Xpath- ‘//*[@id=”btnviewdetails”] using selenium. I have applied all try and exception method to make sure that the code is working.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
def get_element_center_coordinates(element):
location = element.location
size = element.size
center_x = location['x'] + size['width'] / 2
center_y = location['y'] + size['height'] / 2
return center_x, center_y
# URL of the web page
url = "https://tmrsearch.ipindia.gov.in/eregister/"
# Initialize the WebDriver (Make sure to have the correct path to chromedriver)
browser = webdriver.Chrome()
try:
# Open the URL and maximize the window
browser.maximize_window()
browser.get(url)
# Allow the page to load completely
WebDriverWait(browser, 30).until(
EC.presence_of_element_located((By.XPATH, '//body')) # Wait until the body is present
)
print("Page loaded successfully.")
# Add more sleep to ensure everything is loaded (if necessary)
time.sleep(5)
# Print the current page source for debugging
page_source = browser.page_source
print("Current page source:n", page_source)
# Adjust the XPath to the element you want to find
element_xpath = '//*[@id="btnviewdetails"] and text()="Trade Mark Application/Application Details"]' # Locate the element
element = WebDriverWait(browser, 30).until(
EC.presence_of_element_located((By.XPATH, element_xpath))
)
print("Element found.")
# Get the center coordinates of the element
center_x, center_y = get_element_center_coordinates(element)
print(f"Element center coordinates: ({center_x}, {center_y})")
except Exception as e:
print("Exception occurred:", str(e))
finally:
# Close the browser
browser.quit()
I am facing this error-
DevTools listening on ws://127.0.0.1:58581/devtools/browser/e8045f57-8eb5-4e79-9f39-101ab408ec63
Created TensorFlow Lite XNNPACK delegate for CPU.
Exception occurred: Message:
Stacktrace:
GetHandleVerifier [0x00007FF63FD01F52+60322]
(No symbol) [0x00007FF63FC7CEC9]
(No symbol) [0x00007FF63FB37EBA]
(No symbol) [0x00007FF63FB87676]
(No symbol) [0x00007FF63FB8773C]
(No symbol) [0x00007FF63FBCE967]
(No symbol) [0x00007FF63FBAC25F]
(No symbol) [0x00007FF63FBCBC80]
(No symbol) [0x00007FF63FBABFC3]
(No symbol) [0x00007FF63FB79617]
(No symbol) [0x00007FF63FB7A211]
GetHandleVerifier [0x00007FF6400194AD+3301629]
GetHandleVerifier [0x00007FF6400636D3+3605283]
GetHandleVerifier [0x00007FF640059450+3563680]
GetHandleVerifier [0x00007FF63FDB4326+790390]
(No symbol) [0x00007FF63FC8750F]
(No symbol) [0x00007FF63FC83404]
(No symbol) [0x00007FF63FC83592]
(No symbol) [0x00007FF63FC72F9F]
BaseThreadInitThunk [0x00007FFD634E7344+20]
RtlUserThreadStart [0x00007FFD636226B1+33]
I want to retrieve the x and y coordinates of the element using the location Xpath- ‘//*[@id=”btnviewdetails”]’. This is the code for it. Please help me in resolving the error or let me know an alternative method for the same.