Dropdown option not getting selected using Selenium Python - python

I am trying to select a dropdown option in the form using Selenium webdriver in Python. The XPATH is correct, I also verified it is going to the right dropdown option but in the end it is not selecting it.
I have tried similar code for another website that has a dropdown. But it's not working for this particular website.
Can someone please help out with this?
from selenium import webdriver
driver = webdriver.Chrome("C:\\Users\\xxx\\Downloads\\chromedriver_win32\\chromedriver.exe")
driver.get("https://www.cersai.org.in/CERSAI/dbtrsrch.prg")
elem = driver.find_element_by_xpath("//select[#id='borrowerType']")
all_options = elem.find_elements_by_tag_name("option")
for option in all_options:
if option.get_attribute("value") == "IND":
option.click()
break

You should add a wait before accessing the dropdown element to make it loaded.
Also, this is a Select element, you can treat it in a special way as below:
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
driver = webdriver.Chrome("C:\\Users\\xxx\\Downloads\\chromedriver_win32\\chromedriver.exe")
wait = WebDriverWait(driver, 20)
driver.get("https://www.cersai.org.in/CERSAI/dbtrsrch.prg")
wait.until(EC.visibility_of_element_located((By.XPATH, "//select[#id='borrowerType']")))
select = Select(driver.find_element_by_xpath("//select[#id='borrowerType']"))
# select by visible text
select.select_by_value('IND')

It's strange that Select class did not work. It needs a JavaScript call.
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(50)
driver.get("https://www.cersai.org.in/CERSAI/dbtrsrch.prg")
driver.execute_script("return document.getElementById('borrowerType').selectedIndex = '2'")

Related

Find xpath after opened a dropdown menu to python selenium

I currently wish to select a value from a drop-down menu in python with selenium.
The site is the following: https://www.palyazat.gov.hu/tamogatott_projektkereso?fbclid=IwAR3rmPVj-YAVoMTs2Vodj7JKTVIAZkbTiZ9z4b0j04mq2ThECw5kQOI1p7M
My current code
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
driver = webdriver.Safari(executable_path = '/usr/bin/safaridriver')
wait = WebDriverWait(driver, 20)
driver.get("https://www.palyazat.gov.hu/tamogatott_projektkereso?fbclid=IwAR3rmPVj-YAVoMTs2Vodj7JKTVIAZkbTiZ9z4b0j04mq2ThECw5kQOI1p7M")
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[#class='form-group' and .//label[#for='programok']]//div[contains(#class,'css-1wy0on6')]"))).click()
time.sleep(1)
current_program = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(#class,'css-26l3qy-menu')]")))
current_program.click()
My issue is that I am not able to find out the needed xpath code for the pop-upped drop-down menu, since I can see it in developer view only if the list is clicked, but I can see then only that "//div[contains(#class,'css-26l3qy-menu')]" is a suitable xpath for the menu. (see the enclosed picture)
But it chooses a random value. How specify which value I want to select?

Drop down menu isnt working with selenium

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
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).
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 :
how about using
driver.find_element_by_xpath('//*[#id="select_symbol"]/option[3]').click()
It is easy to only change option['number'].

Unable to select element from Dropdown that hides - Python Selenium

I'm trying to get to the results page of a search but I have to first click on the dropdown option to complete the search. When I do this manually, the dropdown hides if I do not click on it right as it appears, when I code it, I get a the following error:
ElementNotInteractableException: Message: Element <div id="_esgratingsprofile_autocomplete-results-container" class="autocomplete-results-container msci-ac-search-data-dropdown"> could not be scrolled into view
This is my code so far, you can visit the url and see how it is yourself as well:
from selenium.webdriver import Firefox
from selenium.webdriver.support.ui import Select
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.set_headless()
assert opts.headless
browser = Firefox(options=opts)
browser.get('https://www.msci.com/esg-ratings')
search_form = browser.find_element_by_id('_esgratingsprofile_keywords')
search_form.send_keys('MSFT')
browser.find_element_by_xpath("//div[#id='_esgratingsprofile_autocomplete-results-container']/ul[#id='ui-id-1']/li[#class='msci-ac-search-section-title ui-menu-item']").click()
I looked through many other answers but they didnt seem to deal with the case where the dropdown was not a directly clickable element or where it hides if you dont click on it right away. Any help is appreciated.
Try the below code, This code is working for me. Let me know if it shows any error.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.maximize_window()
wait = WebDriverWait(driver, 5)
action = ActionChains(driver)
driver.get("https://www.msci.com/esg-ratings")
Drop_Down = driver.find_element_by_xpath('//*[#id="_esgratingsprofile_keywords"]')
Drop_Down.send_keys("MSFT")
# Select the First Result from the search.
Result = wait.until(
EC.presence_of_element_located((By.XPATH, "//div[contains(#class,'autocomplete-results-container')]/ul/li[1]")))
action.move_to_element(Result).click().perform()

How to click on an element from the Dropdown menu through Python and Selenium

I'm trying to click on the drop down menu but with no luck .
the menu is activated by javascript .
I tried to click on the link inside the parent div but nothing happens
here is some code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.energisa.com.br/Paginas/login.aspx")
select_element = Select(driver.find_element_by_id('ddlEstado'))
select_element.select_by_value('MG')
# select by visible text
select_element.select_by_visible_text('MG')
As per the your question the website https://www.energisa.com.br/Paginas/login.aspx the dropdown menu is not with in a Select tag, so Select class won't work here.
Once the url is accessed, you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.energisa.com.br/Paginas/login.aspx")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[#class='estado']/div[#class='select2-container' and #id='s2id_ddlEstado']"))).click()
driver.find_element_by_xpath("//ul[#class='select2-results' and #id='select2-results-1']//li/div[normalize-space()='MG']").click()
The dropdown you are trying to click is not actually a SELECT element so you can't use the Select class. The SELECT you are trying to click is just a backing element but it's invisible so you can't interact with it.
To make this work, you will need to click the dropdown element to expose the options and then click the desired option.
driver.find_element_by_css_selector("#s2id_ddlEstado > a").click()
driver.find_element_by_xpath("//ul[#id='select2-results-1']/li[.='MG']").click()
This is untested code, so you may need to add a wait...
The one which appears like a select_list is not a select_list, the purpose of this kind of select_list is, we can write into the text_field to pick up the elements from the huge list, if you type 'M', then all the options which has M will be displayed.
Write the following code, it would work.
WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.ID,"loadingContent")))
driver.find_element_by_id("s2id_ddlEstado").click
driver.find_element_by_xpath("//ul[#id='select2-results-1']//div[text()='MG']").click

Python/Selenium How to select first option of autocomplete searchbar

I am trying to enter a keyword into a searchbar of this site and select the first autocomplete option that comes up. If I were to do this manually I would type in "remote" in the search bar and press the downkey and press enter which I have tried to replicate in my code below, but instead it seems to be skipping the downkey part and not selecting the first autocomplete option and going to the search results page instead of this page which is the first autocomplete option.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://confluence.eits.uga.edu/dashboard.action")
elem = driver.find_element_by_id("quick-search-query")
elem.send_keys("remote")
elem.send_keys(Keys.ARROW_DOWN)
elem.send_keys(Keys.RETURN)
Anyone have any ideas why the downkey is not being registered or am I not using the right syntax?
You are still sending the keys to the search input that triggers the search. Instead, find the first quick search dropdown option and send keys to it:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://confluence.eits.uga.edu/dashboard.action")
elem = driver.find_element_by_id("quick-search-query")
elem.send_keys("remote")
elem.send_keys(Keys.ARROW_DOWN)
# wait for the first dropdown option to appear and open it
first_option = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".quick-search-dropdown li a")))
first_option.send_keys(Keys.RETURN)
You can also try actionchains in this case as following:
elem = driver.find_element_by_id("quick-search-query")
elem.send_keys("remote")
suggestion = driver.find_element_by_css_selector("#quick-search > fieldset > div > div > ol:nth-child(1) > li:nth-child(1) a > span")
actions = ActionChains(driver)
actions.move_to_element(suggestion )
actions.click(suggestion)
actions.perform()

Categories

Resources