[Selenium]How to click on a hidden link ,move to the drop down menu and click submenu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | < table id="_paid_19" class="GOMainTable" cellspacing="0" cellpadding="0"> < tbody > < tr > < tr > < td style="overflow:hidden;"> < div class="GOBodyMid" style="overflow: hidden; width: 625px; height: 334px;"> < div class="GOPageOne"> < table class="GOSection" cellspacing="0" cellpadding="0"> < tbody > < tr > < tr class="GODataRow " style="height: 26px;" onmousemove="Grids[3].ARow=Grids[3].Rows["AR1"];Grids[3].ASec=0;"> < td style="width:0px;height:0px"></ td > < td class=" GOClassReadOnly highlight-tbl HoverClass GOText GOCell HideCol3displayName" colspan="2" style="">Automation Smoke test Template</ td > < td class=" GOClassReadOnly highlight-tb HoverClass GOHtml GOCell HideCol3editPencil" colspan="2" style=""> < div class="edit-Pencil"></ div > </ td > < td class=" GOClassReadOnly highlight-tb b_r HoverClass GOHtml GOCell HideCol3deleteIcon" colspan="2" style=""> < td class=" GOClassReadOnly highlight-tb b_r HoverClass GODate GOCell HideCol3dateModified" colspan="2" style="">8/11/2014</ td > < td class=" GOClassReadOnly highlight-tb b_r HoverClass GOText GOCell HideCol3owner" colspan="2" style="">Rachel Lv</ td > < td class=" GOClassReadOnly highlight-tbr HoverClass GOText GOCell HideCol3permission" colspan="2" style="">Read Only</ td > < td class=" HoverClass GOHtml GOCellEmpty GOEmpty HideCol3_ConstWidth"> </ td > </ tr > |
场景:需要找到Table中的某一行,然后找到这行的铅笔图标,点这个铅笔图标会出来下拉菜单,然后在下拉菜单中点击Open
刚开始写脚本的时候,按照以往的方式会报这样的错误:"Element is not currently visible and so may not be interacted with"
这个元素在界面上是可见的,也通过正则表达式唯一匹配到了,可是在Click的时候就是不行。
后来查阅资料,发现这个元素的父元素是hidden的。
原文如下:
Selenium determines an element is visible or not by the following criteria (use a DOM inspector to determine what css applies to your element, make sure you look at computed style):
- visibility != hidden
- display != none (is also checked against every parent element)
- opacity != 0 (this is not checked for clicking an element)
- height and width are both > 0
- for an input, the attribute type != hidden
Your element is matching one of those criteria. If you do not have the ability to change the styling of the element, here is how you can forcefully do it by using Actions in the code.
后来我是这样解决的,可以成功运行了。
1 2 3 4 5 6 7 8 9 10 | Actions action = new Actions(driver); WebElement pencilIcon = page.getPencilIcon(templateName); action.moveToElement(pencilIcon).click(); WebElement menu = page.getMenu(); action.moveToElement(menu).build().perform(); Assert.assertTrue(menu.isDisplayed(), "Cannot find the menu" ); WebElement open = page.getOpenMenu(); open.click(); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现