我有一个div
具有唯一的ID。在那div
是一堆span
元素有className=foo
。有几个span
元素与className=foo
,但它们是唯一的每个div
(如果这是明确的)。所以,我的硒代码首先获取独特div
作为网络元素,然后尝试采取元素,并得到由类名span
像这样Selenium WebDriver访问子元素
element = sDriver.findElement(By.id("c_"+cID));
String sTest = element.findElement(By.className("actions")).getText();
在第二行是每次抛出一个异常
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 22 milliseconds
我误解如何从独特的div
下取得span
?
您还可以在此查看[可能的解决方案](http://stackoverflow.com/a/7474518/1077279)。 – shamp00 2012-01-12 15:06:12
这似乎是如果我在睡觉,并等待几秒钟,它工作正常。如果没有好的方法来判断页面是否已经完全加载,我想不幸的是我只需要使用睡眠 – ducati1212 2012-01-12 16:01:45
,您可以同时结合睡眠和捕捉异常。尝试访问这两个元素首先没有睡眠,当它失败时使用睡眠,然后再试一次 – chaosr 2012-01-12 19:55:18