Selenium WebDriver访问子元素 [英] Selenium WebDriver access a sub element
问题描述
我有一个具有唯一ID的div
.在div
下是一堆具有className=foo
的span
元素. className=foo
有多个span
元素,但对于每个div
来说都是唯一的(如果很清楚).因此,我的Selenium代码首先获取唯一的div
作为Web元素,然后尝试采用该元素并通过类名获取span
,如
I have a div
with a unique ID. Under that div
are a bunch of span
elements that have className=foo
. There are several span
elements with className=foo
but they are unique to each div
(if that's clear). So my Selenium code first gets the unique div
as a web element then tries to take that element and get by class name the span
like so
element = sDriver.findElement(By.id("c_"+cID));
String sTest = element.findElement(By.className("actions")).getText();
在第二行,每次都会引发异常
On the second line it throws an exception every time
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
?
Do I misunderstand how to get that span
from under a unique div
?
推荐答案
不是,您正确地访问了跨度,但是问题在于,自StaleReferenceException即将发布以来,Dom已更改(请参阅
Nope you'right accessing the span but the problem is that the Dom has changed since StaleReferenceException is about (see StaleReferenceException )
这可能是由于在代码启动时页面未完全加载或在执行代码时更改页面导致的.您可以尝试等待更长的时间,或者捕获StaleReferenceException并再次尝试查找div和span.
This may be caused because the page isn't loaded completely when the code starts or changes when the code is executed. You can either try to wait a little longer for the element or catch the StaleReferenceException and try again finding the div and the span.
这篇关于Selenium WebDriver访问子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!