菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
454
0

[Selenium]How to click on a hidden link ,move to the drop down menu and click submenu

原创
05/13 14:22
阅读数 68663
<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.

后来我是这样解决的,可以成功运行了。

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();

 

发表评论

0/200
454 点赞
0 评论
收藏