如何使用Selenium Python选择下一个下拉菜单 [英] How to select the next dropdown menu using selenium python

查看:320
本文介绍了如何使用Selenium Python选择下一个下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要选择第二,第三和最后一个下拉菜单selenium python.

I need to select the second, third and up to last dropdown menu selenium python.

我可以选择第一个下拉菜单,但是不知道如何选择下一个下拉菜单. 为了选择第一个下拉菜单,我使用了以下命令

I can select the first dropdown menu, but don't know how to select the next dropdown menu. For selecting the first dropdown menu I used the following command

from selenium import webdriver
driver =webdriver.Firefox()
driver.get("https://pmfby.gov.in")
driver.maximize_window()
em = driver.find_element_by_partial_link_text("Calculator")
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
ActionChains(driver).click(em).perform()
Select(driver.find_element_by_tag_name("select")).select_by_index(2)

网页截图如下:在此处输入图像描述

请帮助我解决此问题.

Please help me to solve this issue.

推荐答案

由于页面上显示的所有下拉列表的下拉placeholder文本均相同,因此您可以使一个通用函数从下拉列表中选择值并调用它在任何需要的地方.

As the dropdown placeholder text is same for all the dropdown present on the page, you can make one generic function to select value from the dropdown and call it wherever needed.

您可以这样做:

def select_dropdown_value(dropdown_number):
    select = Select(driver.find_element_by_xpath("(//select[@placeholder='Select Pack'])["+dropdown_number+"]")))
    select.select_by_index(1)

现在您可以通过发送dropdown_number来调用此函数,它将为其从页面上的下拉列表中选择值.
您可以像下面这样调用此函数:

And now you can call this function by sending the dropdown_number in it for which it will select the value from that dropdown on the page.
You can call this function like:

select_dropdown_value(1) or
select_dropdown_value(2)

在这里,如果需要,您也可以参数化索引号.

And here, you can parameterise the index number as well, if needed.

这篇关于如何使用Selenium Python选择下一个下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆