java - WebDriver 资源管理器菜单滚动

标签 java webdriver selenium-webdriver

我们的网络应用程序 UI 基于 GXT 框架 (GWT+EXT JS),并具有资源管理器(树)类型的菜单。 我想向下滚动以到达资源管理器中的树节点。这是我尝试过的

enter image description here

@Test

public void test() {

    logger.info("*************** Started Create Template ******************");

    String nodeElement = //span[@class='x-tree3-node-text'][contains(text(),'QE Auto Template585 [1]')]";
    WebElement treeNode = driver.findElement(By.xpath(nodeElement));        

    treeNode.click();       

}

即使这个元素存在于 DOM 中,我仍然得到“NoSuchElementException” 有什么指示吗?

我正在使用 selenium v​​2.35/firefox v23

<!DOCTYPE html>
<html class=" ext-strict" style="overflow: hidden;">
<head>
<body class=" ext-gecko ext-windows x-theme-gray">
<div id="x-auto-4" class=" x-viewport x-component" style="width: 1378px; height: 577px;">
<div id="x-auto-5" class=" x-component x-border-layout-ct" style="width: 1378px; height: 577px;">
<div id="x-auto-6" class=" x-component x-border-panel" style="left: 0px; top: 0px; width: 1378px; height: 47px;">
<div id="x-auto-7" class=" x-component x-border-panel x-border-layout-ct" style="left: 0px; top: 47px; width: 1378px; height: 530px;">
<div id="x-auto-8" class=" x-component x-border-panel" style="left: 0px; top: 0px; width: 1378px; height: 37px;">
<div id="x-auto-11" class=" x-panel x-component x-border-panel" style="left: 0px; top: 37px; width: 250px;">
<div id="x-auto-10" class=" x-small-editor x-panel-header x-component x-unselectable" role="presentation" unselectable="on">
<div class="x-panel-bwrap" role="presentation">
<div class="x-panel-body" role="presentation" style="width: 248px; height: 446px;">
<div id="x-auto-2" class="x-tree3-node-ct x-tree3 x-component x-unselectable" role="group" unselectable="on" style="display: block; overflow: auto; width: 248px; height: 445px;" tabindex="0" hidefocus="true" aria-activedescendant="x-auto-2__x-auto-2_x-auto-166">
<div id="x-auto-2_x-auto-3" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-58" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-59" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2__x-auto-2_x-auto-59" class="x-tree3-el " aria-level="1" role="treeitem" unselectable="on">
<div class="x-tree3-node-ct" role="group" unselectable="on" style="display: block;">
<div id="x-auto-2_x-auto-77" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-78" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-79" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-80" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2__x-auto-2_x-auto-80" class="x-tree3-el " aria-level="2" role="treeitem" unselectable="on">
<div class="x-tree3-node-ct" role="group" unselectable="on" style="display: block;">
<div id="x-auto-2_x-auto-106" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-107" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-108" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2__x-auto-2_x-auto-108" class="x-tree3-el " aria-level="3" role="treeitem" unselectable="on">
<div class="x-tree3-node-ct" role="group" unselectable="on" style="display: block;">
<div id="x-auto-2_x-auto-122" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-123" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-124" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2__x-auto-2_x-auto-124" class="x-tree3-el " aria-level="4" role="treeitem" unselectable="on">
<div class="x-tree3-node-ct" role="group" unselectable="on" style="display: block;">
<div id="x-auto-2_x-auto-153" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-154" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-155" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-156" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-157" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-158" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-159" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-160" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-161" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-162" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2__x-auto-2_x-auto-162" class="x-tree3-el " aria-level="5" role="treeitem" unselectable="on">
<img style="height: 18px; width: 72px;" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==">
<img class=" x-tree3-node-joint" border="0" style="width:16px;height:16px;background:url(http://prod_link-linux3:8090/prod_name/prod_name/D0E43497744DF5C818B8EC8DC4EAF7AE.cache.png) no-repeat -66px 0px;" src="http://prod_link-linux3:8090/prod_name/prod_name/clear.cache.gif" onload="this.__gwtLastUnhandledEvent="load";">
<span class="x-tree3-node-check"></span>
<img class=" x-tree3-node-icon" border="0" style="width:16px;height:16px;background:url(http://prod_link-linux3:8090/prod_name/prod_name/BBFBF5027EF10FF70B10FF9D6E0F7BE5.cache.png) no-repeat -208px -16px;" src="http://prod_link-linux3:8090/prod_name/prod_name/clear.cache.gif" onload="this.__gwtLastUnhandledEvent="load";">
<span class="x-tree3-node-text" unselectable="on">Demo_GeneratorDevice</span>
</div>
</div>
<div id="x-auto-2_x-auto-163" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-164" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-165" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-166" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2__x-auto-2_x-auto-166" class="x-tree3-el x-ftree2-selected " aria-level="5" role="treeitem" unselectable="on">
<img style="height: 18px; width: 72px;" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==">
<img class=" x-tree3-node-joint" border="0" style="width:16px;height:16px;background:url(http://prod_link-linux3:8090/prod_name/prod_name/D0E43497744DF5C818B8EC8DC4EAF7AE.cache.png) no-repeat -66px 0px;" src="http://prod_link-linux3:8090/prod_name/prod_name/clear.cache.gif" onload="this.__gwtLastUnhandledEvent="load";">
<span class="x-tree3-node-check"></span>
<img class=" x-tree3-node-icon" border="0" style="width:16px;height:16px;background:url(http://prod_link-linux3:8090/prod_name/prod_name/BBFBF5027EF10FF70B10FF9D6E0F7BE5.cache.png) no-repeat -208px -16px;" src="http://prod_link-linux3:8090/prod_name/prod_name/clear.cache.gif" onload="this.__gwtLastUnhandledEvent="load";">
<span class="x-tree3-node-text" unselectable="on">QE Auto Template585 [1]</span>
</div>
</div>
<div id="x-auto-2_x-auto-167" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-168" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-169" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-170" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-171" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-172" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-173" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-174" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-175" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2__x-auto-2_x-auto-175" class="x-tree3-el " aria-level="5" role="treeitem" unselectable="on">
<img style="height: 18px; width: 72px;" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==">
<img class=" x-tree3-node-joint" border="0" style="width:16px;height:16px;background:url(http://prod_link-linux3:8090/prod_name/prod_name/D0E43497744DF5C818B8EC8DC4EAF7AE.cache.png) no-repeat -66px 0px;" src="http://prod_link-linux3:8090/prod_name/prod_name/clear.cache.gif" onload="this.__gwtLastUnhandledEvent="load";">
<span class="x-tree3-node-check"></span>
<img class=" x-tree3-node-icon" border="0" style="width:16px;height:16px;background:url(http://prod_link-linux3:8090/prod_name/prod_name/BBFBF5027EF10FF70B10FF9D6E0F7BE5.cache.png) no-repeat -208px -16px;" src="http://prod_link-linux3:8090/prod_name/prod_name/clear.cache.gif" onload="this.__gwtLastUnhandledEvent="load";">
<span class="x-tree3-node-text" unselectable="on">Sun_BladeServer_Enclosure</span>
</div>
</div>
</div>
</div>
<div id="x-auto-2_x-auto-125" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-126" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-127" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-128" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-129" class="x-tree3-node" role="presentation" unselectable="on">
</div>
</div>
<div id="x-auto-2_x-auto-109" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-110" class="x-tree3-node" role="presentation" unselectable="on">
</div>
</div>
<div id="x-auto-2_x-auto-81" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-82" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-83" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-84" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-85" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-86" class="x-tree3-node" role="presentation" unselectable="on">
</div>
</div>
<div id="x-auto-2_x-auto-60" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-61" class="x-tree3-node" role="presentation" unselectable="on">
<div id="x-auto-2_x-auto-62" class="x-tree3-node" role="presentation" unselectable="on">
</div>
</div>
<div class="x-panel-footer x-panel-nofooter" role="presentation"></div>
</div>
</div>
<div id="x-auto-12" class=" x-tab-panel x-component x-border-panel" tabindex="0" hidefocus="true" style="left: 253px; top: 37px; width: 1125px;">
<div id="x-auto-13" class=" x-component x-border-panel" style="left: 0px; top: 510px; width: 1378px; height: 20px;">
<div id="x-auto-15" class="x-vsplitbar x-component x-unselectable" style="position: absolute; left: 250px; top: 37px; width: 5px; height: 473px;" unselectable="on"></div>
</div>
</div>
</div>
<iframe id="__gwt_historyFrame" style="position:absolute;width:0;height:0;border:0" tabindex="-1" src="javascript:''">
<noscript> <div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif"> Your web browser must have JavaScript enabled in order for this application to display correctly. </div> </noscript>
<iframe id="prod_name" src="javascript:''" style="position: absolute; width: 0px; height: 0px; border: medium none;" tabindex="-1">
<div style="position: absolute; left: -10000px; top: -10000px; visibility: hidden; font-size: 11px; font-weight: 400; font-style: normal; font-family: arial,tahoma,verdana,helvetica; line-height: 14px; text-transform: none; letter-spacing: normal; width: auto;"></div>
<div class=" " style="position: absolute; left: -10000px; top: -10000px; visibility: hidden;"></div>
<iframe id="x-auto-250" style="position:absolute;width:0;height:0;border:0" name="gxt.formpanel-3">
</body>
</html>

堆栈跟踪

Caused an ERROR
Unable to locate element: {"method":"xpath","selector":"//span[@class='x-tree3-node-text'][contains(text(),'QE Auto Template585 [1]')]"}
Command duration or timeout: 21 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.35.0', revision: '8df0c6b', time: '2013-08-12 15:43:19'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_43'
Session ID: f2828ed2-4dea-4156-b01d-00c63b4ca4ab
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=23.0, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//span[@class='x-tree3-node-text'][text()='Administration']/following::span[@class='x-tree3-node-text'][contains(text(),'QE Auto Template585 [1]')]"}
Command duration or timeout: 21 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.35.0', revision: '8df0c6b', time: '2013-08-12 15:43:19'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_43'
Session ID: f2828ed2-4dea-4156-b01d-00c63b4ca4ab
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=23.0, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:404)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:344)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
    at main.java.com.xx.xxx.xxx.xx.xx.xx.xx.test(xx.java:267)

谢谢

最佳答案

在您尝试查找这些元素之前,这些元素可能尚未完成在 DOM 中的加载。

您可以尝试等待预期条件presenceOfAllElementsLocatedBy:

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(15L, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);

List<WebElement> treeNodes = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("x-tree3-node-text")));

关于java - WebDriver 资源管理器菜单滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18598294/

相关文章:

linux下不同用户下的java系统偏好设置

java - 将 super 变量转换为类类型

ssl - Selenium Webdriver SSL 证书主机名不匹配问题

C# Selenium 或 WebBrowser 替代品模仿人类

java - 如何处理 selenium webdriver 中 ID 为空的弹出窗口?

Java日历日期错误

java - Android - 无法在服务中的计时器内访问 TextView

python - 无法定位元素: python + selennium

java - Selenium 测试花费的时间异常长

java - 无法使用 phantomjs 驱动程序运行 selenium 脚本