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 as:
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:没有这样的元素:无法 locate元素:{“method”:“css selector”,“selector”:“#statusList&gt; div
div.leftpan-button.leftpan-button-active“}(会话信息:chrome = 50.0.2661.102)(驱动程序信息:chromedriver = 2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform = Windows NT 6.3 x86_64)(警告:服务器未提供任何堆栈跟踪 信息)命令持续时间或超时:0毫秒For 有关此错误的文档,请访问: 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功能 [{applicationCacheEnabled = false,rotate = false, mobileEmulationEnabled = false,chrome = {chromedriverVersion = 2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4), userDataDir = C:\用户\ BAYAPR〜1个\应用程序数据\本地\ TEMP \ scoped_dir1612_1332}, takesHeapSnapshot = true,databaseEnabled = false,handlesAlerts = true, hasTouchScreen = false,版本= 50.0.2661.102,platform = WIN8_1, browserConnectionEnabled = false,nativeEvents = true, acceptSslCerts = true,locationContextEnabled = true, webStorageEnabled = true,browserName = chrome,takesScreenshot = true, javascriptEnabled = true,cssSelectorsEnabled = true}]会话ID: 99a467e8f79b50f7c83ab694b88c2a75 ***元素信息:{Using = css selector,value =#statusList&gt; div> div.leftpan-button.leftpan-button-active} at sun.reflect.GeneratedConstructorAccessor8.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown 来自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)at 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个
答案 0 :(得分:0)
尝试以下代码:
WebDriverWait wait = new WebDriverWait(driver, 9);
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.leftpan-button.leftpan-button-active[href$='/main/po/Finalize']")));
driver.findElement(By.cssSelector("div.leftpan-button.leftpan-button-active[href$='/main/po/Finalize']")).click();
让我知道会发生什么。