简体   繁体   English

如何通过Python和Selenium从下拉菜单中单击元素

[英]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 . 菜单由javascript激活。 I tried to click on the link inside the parent div but nothing happens here is some code : 我试图单击父div内的链接,但是这里没有发生任何变化:

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. 根据您的问题,网站https://www.energisa.com.br/Paginas/login.aspx下拉菜单未包含在Select标记中,因此Select类在这里不起作用。

Once the url is accessed, you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution: 一旦访问了URL ,就需要诱使WebDriverWait使所需的元素可单击,并且可以使用以下解决方案:

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. 您尝试单击的下拉列表实际上不是SELECT元素,因此您不能使用Select类。 The SELECT you are trying to click is just a backing element but it's invisible so you can't interact with it. 您尝试单击的SELECT只是一个后备元素,但它是不可见的,因此您无法与其交互。

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. 看起来像select_list的那个不是select_list,这种select_list的目的是,我们可以写到text_field来从巨大的列表中拾取元素,如果您键入“ M”,则所有具有M将显示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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