【问题标题】:Need help locating/clicking a drop down located in table using selenium需要帮助使用 selenium 定位/单击位于表中的下拉列表
【发布时间】:2015-01-02 20:01:48
【问题描述】:

我正在编写一个脚本来定位/单击一个下拉列表,然后使用 selenium 和 python 从所选下拉列表中单击任何项​​目。

下面是代码,如果它正确,希望您的帮助使它变得更好,如果它不正确的方法,请纠正它。

代码片段:

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 sn-p:

<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>

【问题讨论】:

标签: python python-2.7 selenium selenium-webdriver


【解决方案1】:

虽然我不确定我是否理解您遇到(或试图解决)的问题是否足以提供帮助,但我会尝试。

如果我是你,尝试点击列表中的特定项目,我会首先创建这些项目的列表,然后循环遍历它一次,边走边点击。代码可能看起来像这样。

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

至少,如果您只是先创建一个您想要检查的项目列表,您可以取消几个 for loopsSee docs.

这有帮助吗?我看到您已经在找到元素(而不仅仅是单个元素),所以我可能对问题的理解不够充分,无法提供充分的建议。考虑在 cmets 中提供有关您的问题/目标的更多说明或编辑您的问题。谢谢,祝你好运!

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

第二次编辑:

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

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

然后,如上所述,您可以循环这个列表和.click() 每个链接。

for link in list_of_links:
    link.click()

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

【讨论】:

  • 在上面的代码中,我没有考虑到 在找到所有包含下拉项目的列[td],但是,给出的链接下面解释了如果需要定位下拉列表中的项目,还应该考虑
    类。这让我觉得我的方法是否正确teammentordevelopment.wordpress.com/2013/04/20/…@Bee Smears
  • @KK16 你有链接还是只是一个 html 文档?我的理解是不,你不需要使用 div,特别是如果所有嵌套项目共享一个类和一个更显着的特征。使用 xpath 就足够了。
  • 我刚拿到 HTML 文档,我真的没有链接,下面是 HTML 文档的链接,希望对 jsfiddle.net/n5evsaof/4 有所帮助。 jsfiddle.net,有 HTML 和结果(页面和下拉列表的样子,在结果窗口中]
  • @KK16 这可能就足够了。你能给我几个你想要返回的元素的例子吗?
  • @Bee Smears,我的目标是单击表格中的任何行,控制最后一列中的下拉菜单[td],然后单击任何下拉项目[链接到应用程序中的其他页面]。虽然我以某种方式完成了一半,但阅读上面发布的链接让我对我的方法产生了疑问。希望我的问题很清楚。
猜你喜欢
相关资源
最近更新 更多
热门标签