Drop down menu isnt working with selenium

Viewed 192
driver = webdriver.Remote(service.service_url)
timeout = 50
driver.get("https://www.nseindia.com/option-chain")
selection = Select(driver.find_element_by_id('select_symbol'))
time.sleep(2)
selection.select_by_index(3)
time.sleep(2)
driver.quit()

I need to get the call option total from the table on the site for each option in the dropdown menu, but the selected option doesn't load and the table doesn't load. Pls help

2 Answers

Sometimes, Webapps do not show desired web element while handled by automation software/tool such as Selenium.

The workaround I have come to know know is that, in this kind of situation, we need to pass Arrow_down key using automated software and using JS (could be Select class also depends on the website), we can let the application show all the options.

Using Selenium this is how drop down looks :- (Basically it does not show the options).

enter image description here

Now when we use the below code that has Arrow down keys implemented :

Code :-

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(50)
driver.get("https://www.nseindia.com/option-chain")
wait = WebDriverWait(driver, 20)

select = Select(wait.until(EC.presence_of_element_located((By.ID, "equity_optionchain_select"))))
select.select_by_visible_text("NIFTY")

ActionChains(driver).key_down(Keys.ARROW_DOWN).key_up(Keys.ARROW_UP).perform()
driver.execute_script("return document.getElementById('select_symbol').selectedIndex = '3';")

Using Selenium this is how it looks now :

enter image description here

how about using

driver.find_element_by_xpath('//*[@id="select_symbol"]/option[3]').click()

It is easy to only change option['number'].

Related