简体   繁体   English

如何使用python3在下拉菜单中选择特定元素?

[英]How to select a specific element in a dropdown menu using python3?

Sorry about my question in case it is a duplicate. 如果我的问题是重复的,抱歉。 I searched and tried to implement what I found about my problem, but couldn't find something that worked for my case. 我进行搜索并尝试实现对问题的发现,但是找不到适合我的情况的东西。 So here it is: I'm trying to click on a specific element in a dropdown menu. 就是这样:我正在尝试单击下拉菜单中的特定元素。 The html of the dropdown menu is like this: 下拉菜单的html如下所示:

</div>
                            </div>
                        </div>
                        <div class="td col15">
                            <div>
                                Cantone
                            </div>
                            <div class="RadAjaxPanel" id="ctl00_MainContent_ctl00_MainContent_ddl_cantonsPanel">
        <div id="ctl00_MainContent_ddl_cantons" class="RadComboBox RadComboBox_Default" style="width:160px;">
            <table summary="combobox" style="border-width:0;border-collapse:collapse;">
                <tr class="rcbReadOnly">
                    <td class="rcbInputCell rcbInputCellLeft" style="width:100%;"><input name="ctl00$MainContent$ddl_cantons" type="text" class="rcbInput radPreventDecorate" id="ctl00_MainContent_ddl_cantons_Input" value="" readonly="readonly" /></td><td class="rcbArrowCell rcbArrowCellRight"><a id="ctl00_MainContent_ddl_cantons_Arrow" style="overflow: hidden;display: block;position: relative;outline: none;">select</a></td>
                </tr>
            </table><div class="rcbSlide" style="z-index:6000;"><div id="ctl00_MainContent_ddl_cantons_DropDown" class="RadComboBoxDropDown RadComboBoxDropDown_Default " style="display:none;"><div class="rcbScroll rcbWidth" style="width:100%;"><ul class="rcbList" style="list-style:none;margin:0;padding:0;zoom:1;"><li class="rcbItem"></li><li class="rcbItem">AG</li><li class="rcbItem">AI</li><li class="rcbItem">AR</li><li class="rcbItem">BE</li><li class="rcbItem">BL</li><li class="rcbItem">BS</li><li class="rcbItem">FR</li><li class="rcbItem">GE</li><li class="rcbItem">GL</li><li class="rcbItem">GR</li><li class="rcbItem">JU</li><li class="rcbItem">LU</li><li class="rcbItem">NE</li><li class="rcbItem">NW</li><li class="rcbItem">OW</li><li class="rcbItem">SG</li><li class="rcbItem">SH</li><li class="rcbItem">SO</li><li class="rcbItem">SZ</li><li class="rcbItem">TG</li><li class="rcbItem">TI</li><li class="rcbItem">UR</li><li class="rcbItem">VD</li><li class="rcbItem">VS</li><li class="rcbItem">ZG</li><li class="rcbItem">ZH</li></ul></div></div></div><input id="ctl00_MainContent_ddl_cantons_ClientState" name="ctl00_MainContent_ddl_cantons_ClientState" type="hidden" />
        </div>
    </div>

I tried the code below, but it is getting the wrong click for some elements (and the right one for a few other elements) In other words it is not a very precise select. 我尝试了下面的代码,但是单击某些元素会导致错误的单击(单击其他元素会导致正确的单击)。换句话说,这不是一个非常精确的选择。 Here's the code: 这是代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select

# Open the website in chrome
url = 'http://www.asca.ch/Partners.aspx?lang=it'
driver = webdriver.Chrome()
driver.get(url)

# Click the dropdown
cantone = driver.find_element_by_xpath("""//*[@id="ctl00_MainContent_ddl_cantons_Input"]""")
cantone.click()

#find all canotenes in the dropdown list. Omit the first empty entry.
dropdown_list = driver.find_elements_by_xpath("""//div[@id='ctl00_MainContent_ddl_cantons_DropDown']/div/ul[@class='rcbList']/li[@class='rcbItem']""")

cantone_list=[]
for l in dropdown_list:
cantone_list.append(l.text)

cantone.send_keys("AI")
cantone.send_keys(Keys.ENTER)

In my code I'm trying to click the first element found (AI), but instead of AI it selects AG. 在我的代码中,我尝试单击找到的第一个元素(AI),但选择了AG而不是AI。 I don't know what I am doing wrong. 我不知道我做错了什么。 Some help, please? 请帮忙吗?

Try this below code. 请尝试以下代码。

dropdown_list = driver.find_elements_by_xpath("//div[@id='ctl00_MainContent_ddl_cantons_DropDown']/div/ul[@class='rcbList']/li")
print(len(dropdown_list))
for l in range(len(dropdown_list)):

  if "AI" in dropdown_list[l].text:
     time.sleep(1)
     dropdown_list[l].click()
     break

If you want to select "AI" just add this step. 如果要选择“ AI”,只需添加此步骤。

driver.find_element_by_xpath("//div[@id='ctl00_MainContent_ddl_cantons_DropDown']//li[.='AI']").click()

This will select the "AI" directly and efficiently. 这将直接有效地选择“ AI”。

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

相关问题 如何在python3中选择元素的特定值 - how to select specific values of an element in python3 如何在python3中使用selenium在javascrip站点中Select下拉列表? - How to Select a dropdown list in a javascrip site using selenium in python3? 如何找到特定元素和1之间的距离? - Python3 - How to find distance between specific element and 1's? - Python3 如何在多行.csv文件中选择特定零件? (Python3) - How to select a specific part in a .csv file on multiple lines? (Python3) 如何检查特定元素与索引之间的距离? 蟒蛇3 - How to check the distance between a specific element an index? Python3 如何在python3中构造具有特定元素大小的数组? - how to construct an array with specific element size in python3? 使用python3和硒获取表格每一行中下拉菜单的链接 - Get links of dropdown menu in each row of the table with python3 and selenium 如何使用Python Selenium从Bootstrap下拉列表中选择元素? - How do I select an element from a Bootstrap dropdown using Python Selenium? 如何使用 Python 在 Selenium 上的 select 下拉菜单 - How to select dropdown on Selenium using Python 如何通过使用python3在具有XML中特定属性的节点中获取值? - How to get value in nodes with specific attribute in XML by using python3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM