I was trying to retrieve information using the Selenium library in Python, from here.
This is my code, I tried accessing the elements using their XPATHs.
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
# Create a new instance of the Chrome driver
driver = webdriver.Chrome()
# Open the webpage
driver.get("https://tuludictionary.in/dictionary/cgi-bin/web/frame.html")
# Select the 'Tulu' radio button
tulu_radio = driver.find_element(By.XPATH, "/html/body/table/tbody/tr/td[1]/form/input[1]")
tulu_radio.click()
# Select the 'Anywhere' radio button
anywhere_radio = driver.find_element(By.XPATH, "/html/body/table/tbody/tr/td[1]/form/input[6]")
anywhere_radio.click()
# Input "AA" in the text box
search_box = driver.find_element(By.XPATH, "/html/body/table/tbody/tr/td[2]/input[1]")
search_box.send_keys("AA")
# Click on the search button
search_button = driver.find_element(By.XPATH, "/html/body/table/tbody/tr/td[2]/input[2]")
search_button.click()
# Check and process search results
output_area = driver.find_element(By.XPATH, "/html/body/table/tbody/tr/td")
output_text = output_area.text.strip()
# Wait for the results to load
try:
if output_text != "No results found":
# Extract and print the text part of the links
# Wait for the list of links to be displayed
link_elements = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.XPATH, "/html/body/table/tbody/tr/td"))
)
if len(link_elements) > 0:
# Get the text of the links
links_text = [link.text for link in link_elements]
print("List of links:")
for link_text in links_text:
print(link_text)
# Click on the fourth link
fourth_link = link_elements[3]
fourth_link.click()
else:
print("NULL")
result = None
except:
# Failed to retrieve results
print("Failed to retrieve results")
result = None
# Close the browser
driver.quit()
I tried doing a few things, first clicking on the 'Tulu', and 'Anywhere' or 'Starting with' radio buttons. Then passing the text in the textbox, say 'AA', 'JK', and post-clicking the search button, checking whether 'No results found' is displayed on the output area. If yes, NULL is returned, else I return the entire text of the output area (which has multiple links). I also tried clicking on one of the links, say the fourth one.
The code didn't seem to work, and the web driver closed abruptly with the following error.
File "C:\Users\chakr\AppData\Local\Temp\tempCodeRunnerFile.python", line 13, in <module>
tulu_radio = driver.find_element(By.XPATH, "/html/body/table/tbody/tr/td[1]/form/input[1]")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 740, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 346, in execute
self.error_handler.check_response(response)
File "C:\Program Files\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/table/tbody/tr/td[1]/form/input[1]"}
(Session info: chrome=114.0.5735.135); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
Backtrace:
GetHandleVerifier [0x00F5A813+48355]
(No symbol) [0x00EEC4B1]
(No symbol) [0x00DF5358]
(No symbol) [0x00E209A5]
(No symbol) [0x00E20B3B]
(No symbol) [0x00E4E232]
(No symbol) [0x00E3A784]
(No symbol) [0x00E4C922]
(No symbol) [0x00E3A536]
(No symbol) [0x00E182DC]
(No symbol) [0x00E193DD]
GetHandleVerifier [0x011BAABD+2539405]
GetHandleVerifier [0x011FA78F+2800735]
GetHandleVerifier [0x011F456C+2775612]
GetHandleVerifier [0x00FE51E0+616112]
(No symbol) [0x00EF5F8C]
(No symbol) [0x00EF2328]
(No symbol) [0x00EF240B]
(No symbol) [0x00EE4FF7]
BaseThreadInitThunk [0x753C7D59+25]
RtlInitializeExceptionChain [0x7725B74B+107]
RtlClearBits [0x7725B6CF+191]
Could someone point out what is wrong with the code and what should be changed?