0

我有一个 div 元素并想检索它的子元素。为了做到这一点,我需要将它们全部显示出来,所以我决定需要滚动查看它们。问题是,我得到了我的元素,但是当我打电话时:

((JavascriptExecutor) driver).executeScript("arguments[0].scrollBy(0,250)",element);

其中 element 是一个 WebElement 对象我得到这个堆栈跟踪:

org.openqa.selenium.WebDriverException: JavaScript error (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 125 milliseconds
Build info: version: '2.37.0', revision: 'a7c61cb', time: '2013-10-18 17:14:00'
System info: host: 'seleniumRC', ip: '192.168.115.95', os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_45'
Session ID: c74e4f7f-3563-44eb-98a2-4aa6a0347761
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, enablePersistentHover=true, ignoreZoomSetting=false, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=8, ie.usePerProcessProxy=false, cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false, initialBrowserUrl=http://****:64232/, handlesAlerts=true, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]
Command duration or timeout: 1.61 seconds
Build info: version: '2.37.1', revision: 'a7c61cbd68657e133ae96672cf995890bad2ee42', time: '2013-10-21 09:08:07'
System info: host: 'ROTIMN0L022408', ip: '135.247.143.198', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_45'
Session ID: 4b75711b-4e98-47d5-acfc-c26e417b0cb1
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, enablePersistentHover=true, ignoreZoomSetting=false, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, webdriver.remote.sessionid=4b75711b-4e98-47d5-acfc-c26e417b0cb1, version=8, ie.usePerProcessProxy=false, ignoreProtectedModeSettings=false, cssSelectorsEnabled=true, requireWindowFocus=false, initialBrowserUrl=http://*:64232/, handlesAlerts=true, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
    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.executeScript(RemoteWebDriver.java:463)
    at com.motive.hda.selenium.SeleniumSimulatorsTest.test02_checkHDASimulatorEfficiency(SeleniumSimulatorsTest.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.openqa.selenium.remote.ScreenshotException: Screen shot has been taken
Build info: version: '2.37.1', revision: 'a7c61cbd68657e133ae96672cf995890bad2ee42', time: '2013-10-21 09:08:07'
System info: host: 'ROTIMN0L022408', ip: '135.247.143.198', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_45'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:125)
    ... 13 more
Caused by: org.openqa.selenium.WebDriverException: JavaScript error (WARNING: The server did not provide any stacktrace information)
Command duration or enter code here timeout: 125 milliseconds
Build info: version: '2.37.0', revision: 'a7c61cb', time: '2013-10-18 17:14:00'
System info: host: 'seleniumRC', ip: '192.168.115.95', os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_45'
Session ID: c74e4f7f-3563-44eb-98a2-4aa6a0347761
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, enablePersistentHover=true, ignoreZoomSetting=false, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=8, ie.usePerProcessProxy=false, cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false, initialBrowserUrl=http://*t:64232/, handlesAlerts=true, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]
Build info: version: '2.37.1', revision: 'a7c61cbd68657e133ae96672cf995890bad2ee42', time: '2013-10-21 09:08:07'
System info: host: 'ROTIMN0L022408', ip: '135.247.143.198', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_45'
Driver info: driver.version: RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
    at org.openqa.selenium.remote.ErrorHandler.rebuildServerError(ErrorHandler.java:227)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:107)
    ... 13 more
4

2 回答 2

0

你可以试试下面的代码:

//Get div element having scroll bar you want to do
WebElement scrollArea = driver.findElement(By.xpath("//")); 

// Initialize Javascript executor
JavascriptExecutor js = (JavascriptExecutor) driver;

// Scroll inside web element vertically (e.g. 1000 pixel)
js.executeScript("arguments[0].scrollTop = arguments[1];",scrollArea, 1000);    
Thread.sleep(1000);`
于 2014-09-16T09:16:45.900 回答
0

如果页面中已经加载了 div 子项,则无需滚动页面来选择它们。

Incase div 和元素未加载然后滚动尝试下面的代码。

WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," + "document.body.scrollHeight,document.documentElement.clientHeight));");
于 2014-01-08T12:18:45.183 回答