0

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)}")
1
  • If you use selenium properly, you won't need all those sleeps Commented Apr 14 at 12:36

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for this. It helped. Can you help me on how to extract data (Total Traded Quantity, Deliverable Qty and % Dly Qt to Traded Qty) from the table for the specific date? I plan to run this code on a daily basis to collect data of over 500 stocks. So once the code is ready, I can create the list and loop this for every stock. Let me know if you need any clarification.
Glad it helped. You should create a new question explaining the issue, your code trials and where you are stuck. This way you will get help from wider community. Once you have created a new question, share the link here, and I will try to help.
Hi Shawn, I have posted the question stackoverflow.com/questions/79574809/…
1

There's no need to simulate "human type" data entry. You also don't need the undetected Chromedriver for this. And you definitely don't need or want the sleep calls.

Try this:

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
from selenium.webdriver.common.keys import Keys

URL = "https://www.nseindia.com/report-detail/eq_security"

def get_name(driver, token):
    wait = WebDriverWait(driver, 5)
    ec = EC.presence_of_element_located
    sel = (By.ID, "hsa-symbol")
    field = wait.until(ec(sel))
    field.send_keys(token + Keys.ENTER)
    sel = (By.CSS_SELECTOR, "#hsa-symbol_listbox p.line1 span")
    return wait.until(ec(sel)).text

with webdriver.Chrome() as driver:
    driver.get(URL)
    print(get_name(driver, "INFY"))

Output:

Infosys Limited

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.