首页 > 解决方案 > 使用 selenium 单击展开和折叠按钮

问题描述

我有一个组,我必须选择展开图标,然后进一步展开。示例:单击用户目录,然后单击“本机目录”

在此处输入图像描述

我使用类方法单击“用户目录”,效果很好。却无法更进一步。我使用了 Class 方法,因为 xpath,css 选择器不起作用并且没有元素 ID。

a=[]
a=driver.find_elements_by_class_name("bi-tree-view-expand-icon")
a[0].click()

唯一的区别是图片标题 - 展开和折叠

用户名录html代码 在此处输入图像描述

本机目录 html 代码

在此处输入图像描述

HTML 代码

<div class="bi-tree-view-body-content" style="left: 0px; top: 0px; width: 197px; height: 104px;"><table class="bi-tree-view-table" style="width: 197px;" cellspacing="0"><tbody class=" focused"><tr class=" selected lead" style="height: 18px;"><td style="width: 197px; padding-left: 3px;"><nobr><img title="Collapse" class="bi-tree-view-expand-icon" alt="" src="https://hyperionqa24.cgate.com/workspace/cache/2020-02-16_11-33-04/bpmstatic/thirdparty/bindows/html/themes/Skyros/images/tree-view-minus.gif"><img class="icon" alt="" src="https://hyperionqa24.cgate.com/workspace/cache/2020-02-16_11-33-04/bpmstatic/thirdparty/bindows/html/themes/Skyros/images/tree_folder_closed.gif">User Directories</nobr></td></tr><tr style="height: 18px;"><td style="width: 197px; padding-left: 22px;"><nobr><img title="Expand" class="bi-tree-view-expand-icon" alt="" src="https://hyperionqa24.cgate.com/workspace/cache/2020-02-16_11-33-04/bpmstatic/thirdparty/bindows/html/themes/Skyros/images/tree-view-plus.gif"><img class="icon" alt="" src="https://hyperionqa24.cgate.com/workspace/cache/2020-02-16_11-33-04/bpmstatic/thirdparty/bindows/html/themes/Skyros/images/tree_folder_closed.gif">Native Directory</nobr></td></tr><tr style="height: 18px;"><td style="width: 197px; padding-left: 22px;"><nobr><img title="Expand" class="bi-tree-view-expand-icon" alt="" src="https://hyperionqa24.cgate.com/workspace/cache/2020-02-16_11-33-04/bpmstatic/thirdparty/bindows/html/themes/Skyros/images/tree-view-plus.gif"><img class="icon" alt="" src="https://hyperionqa24.cgate.com/workspace/cache/2020-02-16_11-33-04/bpmstatic/thirdparty/bindows/html/themes/Skyros/images/tree_folder_closed.gif">Honeywell</nobr></td></tr><tr style="height: 18px;"><td style="width: 197px; padding-left: 3px;"><nobr><img title="Expand" class="bi-tree-view-expand-icon" alt="" src="https://hyperionqa24.cgate.com/workspace/cache/2020-02-16_11-33-04/bpmstatic/thirdparty/bindows/html/themes/Skyros/images/tree-view-plus.gif"><img class="icon" alt="" src="https://hyperionqa24.cgate.com/workspace/cache/2020-02-16_11-33-04/bpmstatic/thirdparty/bindows/html/themes/Skyros/images/tree_folder_closed.gif">Application Groups</nobr></td></tr><tr style="height: 18px;"><td style="width: 197px; padding-left: 3px;"><nobr><img title="Expand" class="bi-tree-view-expand-icon" alt="" src="https://hyperionqa24.cgate.com/workspace/cache/2020-02-16_11-33-04/bpmstatic/thirdparty/bindows/html/themes/Skyros/images/tree-view-plus.gif"><img class="icon" alt="" src="https://hyperionqa24.cgate.com/workspace/cache/2020-02-16_11-33-04/bpmstatic/thirdparty/bindows/html/themes/Skyros/images/tree_folder_closed.gif">File System</nobr></td></tr><tr class="vertical-filler" style="height: 14px;"><td style="width: 197px;">&nbsp;</td></tr></tbody></table></div>

使用 CSS 选择器: - 用户目录 - .selected > td:nth-child(1) > img:nth-child(1) - 本机目录 -.focused > tr:nth-child(2) > td:nth-child(1) > img:nth-child(1)

标签: pythonhtmlselenium-webdriverweb-scraping

解决方案


推荐阅读