需要帮助来查找/单击使用硒的表格中的下拉菜单 [英] Need help locating/clicking a drop down located in table using selenium

查看:75
本文介绍了需要帮助来查找/单击使用硒的表格中的下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个脚本来查找/单击一个下拉列表,然后使用Selenium和python单击所选下拉列表中的任何项目.

I am working on a script to locate/click a dropdown and the click any of the item from the selected drop down using selenium and python.

下面是代码,希望您的帮助使其变得更正确(如果它正确),并对其进行纠正(如果它的方法不正确).

Below is the code and would like your help to make it better if its correct and correct it if its not correct way of doing it.

代码段:

table1 = self.browser.find_element_by_id('user-list-table')
trows = table1.find_elements_by_tag_name('tr')
for trow in trows:
    tcols = trow.find_element_by_tag_name('td')
    for tcol in tcols:
        if tcol ==("//button[@id='dropdownMenu1'][3]"):
            self.browser.find_element_by_xpath(tcol).click()
            self.browser.find_element_by_link("//a[contains(text(),'Edit User')][3]").click()

HTML片段:

<table class="table table-bordered table-striped datatable" id="user-list-table">
                <thead>
                    <tr>
                        <th>Status</th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Employee ID</th>
                        <th>Patient Load<br>Permanent | Temporary</th>
                        <th>No. of Tasks<br>Priority | Total</th>
                        <th>Tasks Per Day (completed)<br>Week | Month | Quarter</th>
                        <th class="text-right">User Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <tr class="odd">
                        <td class="text-center"><i class="status status-available">01</i></td>
                        <td>Tracy</td>
                        <td>Jones</td>
                        <td>1001</td>
                        <td>10 | 5</td>
                        <td>30 | 50</td>
                        <td>45 | 60 | 50</td>
                        <td class="text-right">
                            <div class="dropdown">
                              <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
                                User Actions
                                <span class="caret"></span>
                              </button>
                              <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                <li role="presentation"><a role="menuitem" tabindex="-1" href="patient-assignment.html"><i class="fa fa-exchange"></i> Assign Patients</a></li>
                                <li role="presentation"><a role="menuitem" tabindex="-1" href="#" data-toggle="modal" data-target="#User-Calendar-Today"><i class="fa fa-calendar"></i> Todays Availability</a></li>
                                <li role="presentation"><a role="menuitem" tabindex="-1" href="user-profile.html"><i class="fa fa-user"></i> View Profile</a></li>
                                <li role="presentation"><a role="menuitem" tabindex="-1" href="user-account.html"><i class="fa fa-pencil"></i> Edit User</a></li>
                                <li role="presentation" class="divider"></li>
                                <li role="presentation"><a role="menuitem" tabindex="-1" href="#"><i class="fa fa-times-circle"></i> Deactivate User</a></li>
                              </ul>
                            </div>
                        </td>
                    </tr>

推荐答案

尽管我不确定我是否了解您所遇到的问题(或试图解决)足够帮助,但我会尽力的.

Although I'm not certain I understand the problem you're experience (or trying to solve) well enough to help, I will try.

如果我是您,尝试单击列表中的特定项目,则只需首先创建这些项目的列表,然后遍历该列表一次,然后单击即可.代码可能看起来像这样.

If I were you, attempting to click specific items from a list, I would just create a list of those items in the first place and then loop through it a single time, clicking as I went. The code might look something like this.

item_list = driver.find_elements_by_xpath('//button[@id="dropdownMenu1"]') #you can add further specifications as necessary

至少,如果您只是首先创建要检查的项目的列表,则可以取消几个for loops. 请参阅文档.

At the very least, you can nix a couple for loops if you would just first create a list of the items you wanted to check. See docs.

这有帮助吗?我发现您已经找到了要素(而不只是一个要素),因此我可能对问题的理解不够充分,无法提供充分的建议.考虑在注释中或编辑问题时对您的问题/目标提供更多的说明.谢谢,祝你好运!

Does that help at all? I see that you're already finding elements (rather than just a single element), so I likely don't understand the problem well enough to advise fully. Consider providing greater clarification about your problem/objective in the comments or editing your questions. Thanks and best of luck!

尽管我的回答中暗示了这一点,但我应该更加清楚.我相信,如果您只是尝试通过循环和检查来到达元素,那么使用XPATH可能是有意义的.它最适合在XML文档中查找非常具体(一次性)的元素.只是我的旺盛.

Although it's implied in my answer, I should be clearer. I believe that if you're simply trying to reach elements through loops and checks, it likely makes sense to use XPATH. it is best suited to finding very specific (one-off) elements in an XML document. Just my tuppence.

第二

以下是您需要的链接列表(我认为):

Here is a list of the links you need (I think):

list_of_links = driver.find_elements_by_xpath('//li[@role="presentation"]/a') # len(list) == 99

然后,如上所述,您可以循环播放此列表并按.click()每个链接.

Then, as said above, you can just loop this one list and .click() each link.

for link in list_of_links:
    link.click()

让我知道它是否有效.祝你好运!

Let me know if it works. Best of luck!!

这篇关于需要帮助来查找/单击使用硒的表格中的下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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