我在html下面我必须悬停第一个元素。 有三个具有相同类名的元素。没有唯一标识符。
这是我的元素的html代码。
<a href="javascript:;" class="nav-link nav-toggle">
<i class="icon-briefcase"></i>
<span class="title">Tender</span>
<span class="arrow open"></span>
<span class="selected"></span>
</a>
我尝试过以下代码,点击我要悬停的第一个元素
List<WebElement> MainMenu=driver.findElements(By.xpath("//a[@class='nav-link nav-toggle']"));
Actions action = new Actions(driver);
action.moveToElement(MainMenu.get(0)).build().perform();
但悬停后有四个元素,其类也相同。没有唯一标识符。
我试过下面的代码
List<WebElement> MainMenu1=driver.findElements(By.xpath("//a[@class='nav-link nav-toggle']"));
Actions action = new Actions(driver);
action.moveToElement(MainMenu.get(0)).build().click();
但无法点击第二个元素。
<!DOCTYPE html>
<html class="ng-scope" data-ng-app="MetronicApp" lang="en">
<head>
<body class="page-header-fixed page-sidebar-closed-hide-logo ng-scope page-content-white page-sidebar-closed" ng-controller="AppController" ng-class="{'page-content-white': settings.layout.pageContentWhite,'page-container-bg-solid': settings.layout.pageBodySolid, 'page-sidebar-closed': settings.layout.pageSidebarClosed}">
<div class="page-spinner-bar hide" ng-spinner-bar="">
<div class="page-header navbar navbar-fixed-top ng-scope" data-ng-include="'tpl/header.html'" data-ng-controller="HeaderController" style="">
<div class="clearfix"> </div>
<div class="page-container">
<div class="page-sidebar-wrapper ng-scope" data-ng-include="'tpl/sidebar.html'" data-ng-controller="SidebarController" style="">
<div class="page-sidebar navbar-collapse collapse ng-scope" ng-show="roleId">
<ul class="page-sidebar-menu page-sidebar-menu-closed" data-keep-expanded="false" data-auto-scroll="true" data-slide-speed="200" ng-class="{'page-sidebar-menu-closed': settings.layout.pageSidebarClosed}" style="">
<li class="nav-item ng-scope" ng-if="roleId != 3 && roleId != 5 && roleId !=7">
<a class="nav-link nav-toggle" href="javascript:;">
<ul class="sub-menu adj_submenu">
</li>
<li class="nav-item ng-scope" ng-if="roleId == 2 || roleId == 6 || roleId == 1">
</ul>
</div>
</div>
<div class="page-content-wrapper" ng-class="(global_user_name != undefined) ? 'page-content-wrapper' : 'page-content'">
<a class="page-quick-sidebar-toggler" href="javascript:;">
<div class="page-quick-sidebar-wrapper ng-scope" data-ng-include="'tpl/quick-sidebar.html'" data-ng-controller="QuickSidebarController" style="">
</div>
<div class="page-footer ng-scope" data-ng-include="'tpl/footer.html'" data-ng-controller="FooterController" style="">
<script src="../assets/global/plugins/jquery.min.js" type="text/javascript">
<script src="../assets/global/plugins/bootstrap/js/bootstrap.min.js" type="text/javascript">
<script src="../assets/global/plugins/bootstrap-hover-dropdown/bootstrap-hover-dropdown.min.js" type="text/javascript">
<script src="../assets/global/plugins/jquery-slimscroll/jquery.slimscroll.min.js" type="text/javascript">
<script src="../assets/global/plugins/jquery.blockui.min.js" type="text/javascript">
<script src="../assets/global/plugins/js.cookie.min.js" type="text/javascript">
<script src="../assets/global/plugins/bootstrap-switch/js/bootstrap-switch.min.js" type="text/javascript">
<script src="../assets/global/plugins/angularjs/angular.min.js" type="text/javascript">
<script src="../assets/global/plugins/angularjs/angular-sanitize.min.js" type="text/javascript">
<script src="../assets/global/plugins/angularjs/angular-touch.min.js" type="text/javascript">
<script src="../assets/global/scripts/angular-animate.min.js" type="text/javascript">
<script src="../assets/global/scripts/toaster.js" type="text/javascript">
<script src="../assets/global/plugins/angularjs/plugins/angular-ui-router.min.js" type="text/javascript">
<script src="../assets/global/plugins/angularjs/plugins/ocLazyLoad.min.js" type="text/javascript">
<script src="../assets/global/plugins/angularjs/plugins/ui-bootstrap-tpls.min.js" type="text/javascript">
<script src="js/main.js" type="text/javascript">
<script src="js/directives.js" type="text/javascript">
<script src="../assets/global/scripts/app.min.js" type="text/javascript">
<script src="../assets/layouts/layout/scripts/layout.min.js" type="text/javascript">
<script src="../assets/layouts/global/scripts/quick-sidebar.min.js" type="text/javascript">
<script src="../assets/layouts/global/scripts/quick-nav.min.js" type="text/javascript">
<script src="../app/masters/commonServices/commonService.js" type="text/javascript">
<script src="../app/lib/ng-file-upload.js" type="text/javascript">
<script src="../app/lib/ng-file-upload-shim.js" type="text/javascript">
<script src="../app/lib/d3.min.js" type="text/javascript">
<script src="../app/lib/nv.d3.min.js" type="text/javascript">
<script src="../app/lib/angular-nvd3.min.js" type="text/javascript">
<script src="../app/tender/commonTenderController/commonTender.controller.js" type="text/javascript">
<div id="toast-container" class="toast-top-right" ng-class="[config.position, config.animation]" toaster-options="{'close-button':false, 'time-out':{ 'toast-warning': 2000, 'toast-error': 0 } }"></div>
</body>
</html>
答案 0 :(得分:0)
验证下面的招标xpath是否正确。
//span[contains(text(),'Tender')]/ancestor::a[@class='nav-link nav-toggle']
如果是,则检查html结构是否RPF是投标的直接子项。如果是,则此xpath应该起作用
//(span[contains(text(),'Tender')]/ancestor::a[@class='nav-link nav-toggle'])/a
or
//a[text()[normalize-space()= 'RPF']]"
如果这两个xpath工作,那么你的最终代码将是
Actions action = new Actions(driver);
WebElement tender = driver.findElement(By.xpath("//span[contains(text(),'Tender')]/ancestor::a[@class='nav-link nav-toggle']"));
WebElement rpf= driver.findElement(By.xpath("//(span[contains(text(),'Tender')]/ancestor::a[@class='nav-link nav-toggle'])/a"));
WebElement sighting = driver.findElement(By.xpath("//a[text()[normalize-space()= 'Sighting']]"));
action.moveToElement(tender).moveToElement(rpf).moveToElement(sighting).click().build().perform();
我能够获得两个元素,即嫩和rfp,但我无法找到最后一个目击。
答案 1 :(得分:0)
Tender: //span[text()='Tender']
RFP: //i[contains(text(),'RFP']
Sighting: //i[text()=' Sighting ']
使用这些元素移动到元素并单击瞄准。