使用带有 Java 的 Selenium Webdriver 滚动 Webelement
Scrolling Webelement using Selenium Webdriver with Java
请帮助我处理 StaleElementReferanceException。
我正在尝试使用操作 class 滚动网络元素。
下面是代码:
public void newsSearch() throws StaleElementReferenceException{
driver.get("https://pi.persistent.co.in");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("ui-accordion-1-header-2")).click();
Actions act = new Actions(driver);
int numOfPixels = 2;
WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("//*[@id='ui-accordion-1-panel-2']/div/div[2]/div[2]/div"));
for (int i = 2; i < 50; i = i + numOfPixels) {
act.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0, numOfPixels).release().perform();
System.out.println("Inside for loop");
}
System.out.println("After for loop");
driver.findElement(By.xpath("//*[@id='ulNotification']/li[5]/h3")).click();
driver.close();
}
出现异常:
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 30.16 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50'
System info: host: 'BHD22524', ip: '10.11.66.6', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_55'
Session ID: 03f8c7ca-a582-41b5-80d3-2ec53de719f5
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=37.0.1}]
第 50 行即
act.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0, numOfPixels).release().perform();
我试过用try catch,还是不行。
提前谢谢你。
在迭代次数的 for 循环条件中出现异常?
因为一旦元素被移动或拖动,页面 DOM 将更新并且该元素将无法再移动。这就是您获得 StaleElementReferenceException
的原因。请参阅有关 StaleElementReferenceException
的解释
您移动元素的代码应如下所示:
act.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0, numOfPixels).release().build().perform();
请帮助我处理 StaleElementReferanceException。 我正在尝试使用操作 class 滚动网络元素。 下面是代码:
public void newsSearch() throws StaleElementReferenceException{
driver.get("https://pi.persistent.co.in");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("ui-accordion-1-header-2")).click();
Actions act = new Actions(driver);
int numOfPixels = 2;
WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("//*[@id='ui-accordion-1-panel-2']/div/div[2]/div[2]/div"));
for (int i = 2; i < 50; i = i + numOfPixels) {
act.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0, numOfPixels).release().perform();
System.out.println("Inside for loop");
}
System.out.println("After for loop");
driver.findElement(By.xpath("//*[@id='ulNotification']/li[5]/h3")).click();
driver.close();
}
出现异常:
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 30.16 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50'
System info: host: 'BHD22524', ip: '10.11.66.6', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_55'
Session ID: 03f8c7ca-a582-41b5-80d3-2ec53de719f5
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=37.0.1}]
第 50 行即
act.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0, numOfPixels).release().perform();
我试过用try catch,还是不行。 提前谢谢你。
在迭代次数的 for 循环条件中出现异常?
因为一旦元素被移动或拖动,页面 DOM 将更新并且该元素将无法再移动。这就是您获得 StaleElementReferenceException
的原因。请参阅有关 StaleElementReferenceException
您移动元素的代码应如下所示:
act.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0, numOfPixels).release().build().perform();