I am trying to access data from the "https://www.nseindia.com/report-detail/eq_security" but once I enter a symbol for example "INFY", I have to select the stock from the dropdown. I tried selenium to click on the first option from the dropdown since symbols are unique, so first option should be my stock. But it is not able to view the dropdown. Can anyone suggest how do I click the first option from the dropdown and then get the data from the table appearing below.
I have so far build below:
import undetected_chromedriver as uc
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
import random
# Start undetected Chrome
driver = uc.Chrome()
driver.get("https://www.nseindia.com/report-detail/eq_security")
# Wait for the page to load
time.sleep(5)
# Step 1: Find the symbol input field
symbol_input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="hsa-symbol"]'))
)
# Step 2: Click on it and type 'INFY' with delays
symbol_input.click()
time.sleep(1)
# Human-like typing
for char in "INFY":
symbol_input.send_keys(char)
time.sleep(random.uniform(0.3, 0.6))
# Step 3: Wait for the dropdown to be visible
try:
# Wait for the dropdown to be visible
dropdown = WebDriverWait(driver, 15).until(
EC.visibility_of_element_located((By.XPATH, '//ul[contains(@class, "ui-autocomplete")]'))
)
# Print the HTML of the dropdown to verify it's visible
dropdown_html = driver.page_source
if 'ui-autocomplete' in dropdown_html:
print("✅ Dropdown is visible.")
else:
print("❌ Dropdown not visible.")
# Locate all the dropdown items
dropdown_items = driver.find_elements(By.XPATH, '//ul[contains(@class, "ui-autocomplete")]/li')
if dropdown_items:
print("✅ Dropdown items found.")
for item in dropdown_items:
print(item.text) # Print all options
# Ensure the first item is in view
driver.execute_script("arguments[0].scrollIntoView();", dropdown_items[0])
# Click the first item using JavaScript
driver.execute_script("arguments[0].click();", dropdown_items[0])
print("✅ Clicked on the first dropdown item.")
else:
print("❌ No dropdown items found.")
except Exception as e:
print(f"❌ Error: {str(e)}")
You have complicated the code. See the simple working code below:
import undetected_chromedriver as uc
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
driver = uc.Chrome()
driver.get("https://www.nseindia.com/report-detail/eq_security")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
char = "INFY"
wait.until(EC.element_to_be_clickable((By.ID, "hsa-symbol"))).send_keys(char)
# Below line will click the first dropdown item
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@id='hsa-symbol_listbox']//div//div[1]"))).click()
time.sleep(10)
Note: Below XPath expression will select first dropdown item:
//div[@id='hsa-symbol_listbox']//div//div[1]
Below will select the second item:
//div[@id='hsa-symbol_listbox']//div//div[2]
And so on.