How to select a drop-down menu value with Selenium using Python?
Unless your click is firing some kind of ajax call to populate your list, you don't actually need to execute the click.
Just find the element and then enumerate the options, selecting the option(s) you want.
Here is an example:
from selenium import webdriver
b = webdriver.Firefox()
b.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click()
You can read more in:
https://sqa.stackexchange.com/questions/1355/unable-to-select-an-option-using-seleniums-python-webdriver
Click through dropdown menu with selenium
This is what I found to work. It was not easy to find a unique locator for "Study set" inside the dropdown. There are two "Study set" elements on the page and the first one in the DOM is not visible. I added the waits just to be safe since you are clicking and the dropdown has to load. You may not need the waits but it won't hurt to have them (it won't slow anything down) just in case.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Create']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.UIOverlayTrigger-content svg[aria-label='sets'] + span"))).click()
How to click on an element from the Dropdown menu through Python and Selenium
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()
Click Drop Down Menu with Selenium in Python
You were pretty close. The <span>
tag is not the immediate child so you need to replace >
with a space character and you can use either of the following Locator Strategies:
Using
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[id$='mainContent_MainHeader_HeaderSection_lnkLogout'] span"))).click()
Using
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@id, 'mainContent_MainHeader_HeaderSection_lnkLogout')]//span[text()='Log Out']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Click an item in a drop-down menu using selenium Python library
I've found a solution. Since the drop down menu is coded as "input" HTML tag traditional select Python method doesn't work as it works only with "select" HTML tag. But .send_keys method works just fine. So a simple code bellow does the trick:
variable_name = driver.find_element_by_xpath('xpath of an <input> element in HTML code')
variable_name.click() #Clicks on the drop down button
variable_name.send_keys("text of an option in the drop down list") #sends text of an option in the drop down
Related Topics
Reading Columns of a Txt File on Python
Printing Even Characters With Strings in Python
How to Split Folder of Images into Test/Training/Validation Sets With Stratified Sampling
How to Copy a File to a Remote Server in Python Using Scp or Ssh
Changing Value in Data Frame Column in a Loop Python
How to Crop the Black Background of the Image Using Opencv in Python
How to Get the Url of the Active Google Chrome Tab in Windows
Python - Ensuring a Variable Holds a Positive Number
Login to a Website Using Script
Using Opencv to Overlay Transparent Image Onto Another Image
Discord.Py | Add Role to Someone
Read CSV from Google Cloud Storage to Pandas Dataframe
Iterating Over Every Two Elements in a List
Pyodbc Error Data Source Name Not Found and No Default Driver Specified Paradox
Django Development Server, How to Stop It When It Run in Background
How to Generate and Open an Outlook Email With Python (But Do Not Send)
Matplotlib: Attributeerror: 'Axessubplot' Object Has No Attribute 'Add_Axes'