我正在使用AndroidDriver测试移动网站。我必须首先从侧面菜单中选择语言,然后继续测试搜索向导。
我正在做的是,我首先点击侧边菜单,其中显示了许多选项。然后我点击语言菜单打开一个下拉菜单,我也可以选择我的首选语言。
问题在于,在我选择语言并返回主页以进一步测试搜索向导后,我的测试崩溃说Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: WebElement is stale.
我无法理解为什么会发生这种情况。下面是堆栈跟踪
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: WebElement is stale.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'localhost', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'armv7l', os.version: '3.4.0-ge09aff8', java.version: '0'
Driver info: driver.version: AndroidWebDriver
Command duration or timeout: 4.04 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15'
System info: host: 'DELC02L605GFFT0.sea.corp.expecn.com', ip: '172.26.46.163', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9.2', java.version: '1.6.0_65'
Session ID: 9f47d51d-4dc1-47b3-9219-17ac09335c08
Driver info: org.openqa.selenium.android.AndroidDriver
Capabilities [{platform=ANDROID, javascriptEnabled=true, acceptSslCerts=true, browserName=android, rotatable=true, locationContextEnabled=true, webdriver.remote.sessionid=9f47d51d-4dc1-47b3-9219-17ac09335c08, version=18, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, hasTouchScreen=true, applicationCacheEnabled=true, takesScreenshot=true}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
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:595)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:79)
at org.openqa.selenium.support.ui.Select.setSelected(Select.java:318)
at org.openqa.selenium.support.ui.Select.selectByValue(Select.java:203)
at com.pb.crawler.BcomCrawler.main(MyTestClass.java:28)
Caused by: org.openqa.selenium.StaleElementReferenceException: WebElement is stale.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'localhost', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'armv7l', os.version: '3.4.0-ge09aff8', java.version: '0'
Driver info: driver.version: AndroidWebDriver
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15'
System info: host: 'DELC02L605GFFT0.sea.corp.expecn.com', ip: '172.26.46.163', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9.2', java.version: '1.6.0_65'
Driver info: driver.version: AndroidWebDriver
at org.openqa.selenium.android.library.AndroidWebDriver.throwIfError(AndroidWebDriver.java:1015)
at org.openqa.selenium.android.library.AndroidWebDriver.executeRawScript(AndroidWebDriver.java:895)
at org.openqa.selenium.android.library.AndroidWebDriver.executeAtom(AndroidWebDriver.java:910)
at org.openqa.selenium.android.library.AndroidWebElement.isDisplayed(AndroidWebElement.java:392)
at org.openqa.selenium.android.library.AndroidWebElement.getCenterCoordinates(AndroidWebElement.java:103)
at org.openqa.selenium.android.library.AndroidWebElement.click(AndroidWebElement.java:131)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement$1.invoke(EventFiringWebDriver.java:331)
at $Proxy2.click(Native Method)
at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.click(EventFiringWebDriver.java:344)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at org.openqa.selenium.remote.server.KnownElements$1.invoke(KnownElements.java:63)
at $Proxy3.click(Native Method)
at org.openqa.selenium.remote.server.handler.ClickElement.call(ClickElement.java:29)
at org.openqa.selenium.remote.server.handler.ClickElement.call(ClickElement.java:1)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:170)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
以下是正在运行的代码:
AndroidDriver ad=new AndroidDriver();
System.out.println("Started");
ad.get("my webpage url");
Thread.sleep(2000);
System.out.println("Application Title"+ ad.getTitle());
ad.findElement(By.id("side_menu_btn")).click();
ad.findElement(By.id("langList")).click();
Select select = new Select(ad.findElement(By.id("langList")));
select.selectByValue("UK"); // line number 28
Thread.sleep(2000);
ad.navigate().refresh();
ad.findElement(By.id("input_destination")).sendKeys("Las Vegas, Nevada, U.S.A");
new WebDriverWait(ad, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("li.city"))).click();
ad.findElement(By.id("ci_date_field")).click();
ad.findElement(By.id("ci_date_field")).click();
ad.findElement(By.id("co_date_field")).click();
ad.findElement(By.id("co_date_field")).click();
ad.findElement(By.id("submit_search")).click();
System.out.println("Opened");
System.out.println("second Title"+ ad.getCurrentUrl());
}
请帮助我理解这里的错误以及如何解决这个问题。
答案 0 :(得分:0)
对于抛出错误的任何元素,请执行以下操作:
(new WebDriverWait(driver, 10))
.until(ExpectedConditions.stalenessOf(element));
element.click();
<强>更新强>
根据记录的here,找出元素,抛出staleElementException
然后重新定位它。我认为,我们可以像下面这样实现这一点,这里我们假设ad.findElement(By.id("langList")))
是抛出异常的元素:
(new WebDriverWait(driver, 10)) .until(ExpectedConditions.stalenessOf(ad.findElement(By.id("langList")))); //wait for element to become stale, actually this is element for which the exception is thrown
WebElement element = ad.findElement(By.id("langList"))); //then relocate it again and use it wherever you want
答案 1 :(得分:0)
现在我不再支持Android驱动程序了。请通过webdriver参考selendroid(http://selendroid.io/)获取android自动化。