【发布时间】:2016-10-07 09:48:03
【问题描述】:
HTML代码:
<div class="leftpan-button leftpan-button-active" ui-sref="main.poList({status: 'Finalize'})" ui-sref-active="leftpan-button-active" title="" href="#/main/po/Finalize">Finalize <!-- ngIf: vm.statusCount.PendingCount --><span class="badge pull-right ng-scope" style="background-color: rgb(255, 106, 0);" ng-if="vm.statusCount.PendingCount"></span><!-- end ngIf: vm.statusCount.PendingCount --></div>
完整的 div:
Status list :<div class="panel-body tagList"><div class="leftpan-button leftpan-button-active" ui-sref="main.poList({status: 'Finalize'})" ui-sref-active="leftpan-button-active" title="" href="#/main/po/Finalize">Finalize <!-- ngIf: vm.statusCount.PendingCount --><span class="badge pull-right ng-scope" style="background-color: rgb(255, 106, 0);" ng-if="vm.statusCount.PendingCount"></span><!-- end ngIf: vm.statusCount.PendingCount --></div><div class="leftpan-button" ui-sref="main.poList({status: 'Pending for Approval'})" ui-sref-active="leftpan-button-active" title="" href="#/main/po/Pending%20for%20Approval">Pending for approval <!-- ngIf: vm.statusCount.PendingCount --><span class="badge pull-right ng-binding ng-scope" style="background-color: rgb(255, 106, 0);" ng-if="vm.statusCount.PendingCount" ng-bind="vm.statusCount.PendingCount">11</span><!-- end ngIf: vm.statusCount.PendingCount --></div><div class="leftpan-button" ui-sref="main.poList({status: 'Draft'})" ui-sref-active="leftpan-button-active" title="" href="#/main/po/Draft">Upcoming <!-- ngIf: vm.statusCount.UpcomingCount --><span class="badge pull-right ng-binding ng-scope" style="background-color: rgb(255, 106, 0);" data-ng-if="vm.statusCount.UpcomingCount" ng-bind="vm.statusCount.UpcomingCount">10</span><!-- end ngIf: vm.statusCount.UpcomingCount --></div><div class="leftpan-button" ui-sref="main.poList({status: 'Approved'})" ui-sref-active="leftpan-button-active" title="" href="#/main/po/Approved">Approved <!-- ngIf: vm.statusCount.ApprovedCount --><span class="badge pull-right ng-binding ng-scope" style="background-color: rgb(255, 106, 0);" data-ng-if="vm.statusCount.ApprovedCount" ng-bind="vm.statusCount.ApprovedCount">38</span><!-- end ngIf: vm.statusCount.ApprovedCount --></div><div class="leftpan-button" ui-sref="main.poList({status: 'Rejected'})" ui-sref-active="leftpan-button-active" title="" href="#/main/po/Rejected">Rejected <!-- ngIf: vm.statusCount.RejectedCount --><span class="badge pull-right ng-binding ng-scope" style="background-color: rgb(255, 106, 0);" data-ng-if="vm.statusCount.RejectedCount" ng-bind="vm.statusCount.RejectedCount">9</span><!-- end ngIf: vm.statusCount.RejectedCount --></div></div>
我想点击下面的Div,它是一个链接文本。
我也尝试使用 xpath、Css 选择器和链接,但出现以下错误: 我试过 xpath 为:
driver.findElement(By.xpath("//*[@id='statusList']/div/div[1]").click();
CSS 为:
driver.findElement(By.cssSelector("#statusList > div > div.leftpan-button.leftpan-button-active")).click();
链接为:
driver.findElement(By.linkText("Finalize")).click();
等待:
WebDriverWait wait = new WebDriverWait(driver, 30);// 1 minute
wait.until(ExpectedConditions.visibilityOfElementLocated (By.cssSelector("#statusList > div > div.leftpan-button.leftpan-button-active")));
结果:
org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80) 在 org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:261) 在 com.example.tests.FinalizePO.main(FinalizePO.java:41) 引起: org.openqa.selenium.NoSuchElementException:没有这样的元素:无法 定位元素:{"method":"css selector","selector":"#statusList > div
div.leftpan-button.leftpan-button-active"}(会话信息:chrome=50.0.2661.102)(驱动程序信息:chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),平台=Windows NT 6.3 x86_64) (警告:服务器未提供任何堆栈跟踪 信息)命令持续时间或超时:0毫秒对于 有关此错误的文档,请访问: http://seleniumhq.org/exceptions/no_such_element.html 构建信息: 版本:'2.53.0',修订:'35ae25b',时间:'2016-03-15 16:57:40' 系统信息:主机:'admin',ip:'192.168.51.205',os.name:'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_60' 驱动信息: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, 可旋转=false, mobileEmulationEnabled=false,chrome={chromedriverVersion=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4), userDataDir=C:\Users\BAYAPR~1\AppData\Local\Temp\scoped_dir1612_1332}, 需要HeapSnapshot=true,databaseEnabled=false,handlesAlerts=true, hasTouchScreen=false,版本=50.0.2661.102,平台=WIN8_1, browserConnectionEnabled=false,nativeEvents=true, acceptSslCerts=true,locationContextEnabled=true, webStorageEnabled=true,browserName=chrome,takeScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] 会话 ID: 99a467e8f79b50f7c83ab694b88c2a75 *** 元素信息:{Using=css selector, value=#statusList > div > div.leftpan-button.leftpan-button-active} at sun.reflect.GeneratedConstructorAccessor8.newInstance(未知来源) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 来源)在 java.lang.reflect.Constructor.newInstance(未知来源) 在 org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) 在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363) 在 org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:492) 在 org.openqa.selenium.By$ByCssSelector.findElement(By.java:430) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355) 在 org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:899) 在 org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:897) 在 org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205) 在 org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:1) 在 org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:238) ... 1 更多
【问题讨论】:
-
我看到你的 HTM div 没有 id
statusList存在.. -
@Software_Engineer 包含 statusList 的元素将是树上方的一对元素。
-
你能试试这个定位器吗:
driver.findElement(By.cssSelector("div[ng-if = "vm.statusCount.PendingCount"")).click();。您可能需要修正引号。 -
现在我添加在完整的div下,请检查。
-
您是否尝试过将
contains与ui-sref或ui-sref-active... 结合使用?
标签: java selenium selenium-webdriver