简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM