0

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')
1
  • To rule the obvious out - if the menu is not disabled, you have to enable it before you can click on anything in it, even with Selenium. Commented Aug 11, 2018 at 1:58

3 Answers 3

2

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()
Sign up to request clarification or add additional context in comments.

4 Comments

sorry about that my internet went down . one line of your code that I don't understand normalize-space() also what if i used css selectors !
If you crosscheck the element with the text MG, it's not an innerHTML, so to safeguard from blank spaces we need to use normalize-space(). You can use css selectors as well.
@ahmed.soli Can you raise a new question with your new requirement please? Thanks
@ahmed.soli This question itself is a complete usecase. As you have received well researched answers you shouldn't change the question as per stackoverflow standards. If your requirement have changed feel free to raise a new question. Stackoverflow volunteers will be happy to help you out.
0

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...

1 Comment

I've already tried this approach but I got the error the element is not clickable
0

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

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.