单击 AngularJS 中的下拉列表以获取除使用 Python 和 Selenium 选择以外的元素

Nicolas Corthorn Errázuriz

我正在使用 Selenium 来抓取这个网页在页面中,我可以单击一个按钮并显示一个下拉列表。我希望能够从我的程序列表中选择不同的值。

经过大量研究,我发现通常在 html 中有一个 select 元素,但事实并非如此。我很确定应该单击的元素是以下元素:

<div class="ui-select-container ui-select-bootstrap dropdown ng-valid ng-touched" ng-class="{open: $select.open}" ng-disabled="form.disabledSemanal" ng-change="getSemanaByYear(form.agno.code, form.fechaTermino)" tagging="" tagging-label="('new')" ng-model="form.agno" style="width: 70px" theme="bootstrap" title="Seleccionar año">

我的另一个嫌疑人是:

<i class="caret pull-right" ng-click="$select.toggle($event)"></i>

这是按钮的小箭头。

我可以使用以下代码找到这两个元素:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "https://www.odepa.gob.cl/precios/precios-al-consumidor-en-linea"
driver = webdriver.Firefox()
driver.get(url)
# I have to go through an iframe first
frame_src = "'//aplicativos.odepa.gob.cl/precio-consumidor/serie-precio'"
frame = driver.find_element_by_xpath('//iframe[@src='+frame_src+']')
driver.switch_to.frame(frame)
# Now I find both elements
element1 = driver.find_element_by_xpath("//div[@class='ui-select-container ui-select-bootstrap dropdown ng-valid']")
element2 = driver.find_element_by_xpath("//i[@class='caret pull-right']")

由于这些元素不是选择元素,我无法使用方便的:

from selenium.webdriver.support.ui import Select
select = Select(element1)
select.select_by_visible_text("Some_value")

一种解决方案是使用element1.click(),但不显示下拉列表。

另一种解决方案是等待单击,如下所示:

element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='form-control ui-select-search ng-pristine ng-untouched ng-valid ng-hide']")))
element.click()

但我没有显示列表。

我是否使用了正确的元素?我怎样才能完成这个任务?

萨蒂什

我认为您不能使用 Select 类,因为您要查找的元素不是 Select 类型。

试试这个方法,它对我有用。您必须单击覆盖元素才能显示输入元素。

如果你同意这个批准,用 webdriver wait 替换可怕的 time.sleep 并用更好的方法替换 Keys

from selenium.webdriver import Firefox
from selenium.webdriver.common.keys import Keys
import time

driver = Firefox()
driver.get('https://www.odepa.gob.cl/precios/precios-al-consumidor-en-linea')

time.sleep(5)
# Switch
frame = driver.find_element_by_css_selector(
           'iframe[src="//aplicativos.odepa.gob.cl/precio-consumidor/serie-precio"]'
        )
driver.switch_to.frame(frame)

time.sleep(2)
tipo_de_serie = driver.find_element_by_css_selector('span[aria-label="Seleccionar 
                 tipo de serie activate"]'
                )
driver.execute_script('arguments[0].click();', tipo_de_serie)

time.sleep(2)
seleccionar_tipo_de_serie = driver.find_element_by_css_selector('input[aria- 
                             label="Seleccionar tipo de serie"]'
                            )
seleccionar_tipo_de_serie.send_keys('Anual')
seleccionar_tipo_de_serie.send_keys(Keys.ARROW_DOWN)
seleccionar_tipo_de_serie.send_keys(Keys.ENTER)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Selenium和Python无法定位和单击元素

如何在 Selenium/Python 中单击和打印多个元素?

如何使用Selenium和Python单击<svg:image>元素

如何使用Selenium和Python单击启用GWT的元素

如何使用 Python 和 Selenium 单击此元素?

如何使用Selenium和Python单击文本为Contact的元素

似乎无法使用Selenium和python单击元素

如何使用 Selenium 和 Java 从非选择下拉列表中单击并选择一个选项

如何使用Selenium和Python单击链接

无法使用python和selenium单击链接

如何使用Selenium Webdriver(Python)随机选择并单击元素?

ElementClickInterceptedException:消息:元素单击被拦截元素不可单击错误,使用Selenium和Python单击单选按钮

使用 Selenium 和 Python 访问网页时,Selenium 无法单击元素

如何使用Selenium和Python从列表中获取特定元素

selenium.common.exceptions.ElementClickInterceptedException:消息:拦截了元素单击:Selenium和Python无法单击元素

如何在 python 中使用 Selenium 包单击多个复选框和下拉列表?

如何在不单击每个元素的情况下获得下拉选项(Selenium和Python)

为什么不能使用python和Chrome Webdriver单击Selenium中的一个元素?

无法使用Python Selenium单击元素

如何使用Selenium Python单击元素

使用 selenium,python 查找并单击元素

当微调器使用Selenium和Python将锚元素隐藏时,如何单击该锚元素?

Python 和 Selenium:无法找到要单击的特定 (href) 元素

无法滚动元素以查看和单击。Python Selenium

按标题查找和单击元素Python Selenium

如何通过Selenium和Python按html单击元素

单击带有Selenium和python的CSS元素

selenium.common.exceptions.NoSuchWindowException:消息:使用Selenium和Python单击iframe中的元素没有此类窗口错误

如何使用Selenium和python从下拉列表中获取值列表