I have this html:
<div><div class="header">
<img class="header-map" src="/assets/preview/header_map.png">
<img class="logo-img" src="/.png">
<p class="title">2014-11-06-093755</p>
<p class="address">דיזנגוף 40, תל אביב - יפו</p>
</div></div>
I have this code:
WebElement element = driver.findElement(By.cssSelector("#preview-header > div > div > p.address"));
When I debug on run time I see element
is of type RemoteWebElement
and it has strange id 0.01973947323858738-2
and text empty string.
element.getText()
returns ""
element.getAttribute("class")
returns "address"
So it findes the right element, and there is no need for wait
but still I cannot get the text
When I use Chrome inspect element tool
I get an element with text
$("#preview-header > div > div > p.address").text()
What does it mean RemoteWebElement
? can this be the reason I get empty string as getText()
?
I have this html:
<div><div class="header">
<img class="header-map" src="/assets/preview/header_map.png">
<img class="logo-img" src="https://w-assets.s3.amazonaws./2x/.png">
<p class="title">2014-11-06-093755</p>
<p class="address">דיזנגוף 40, תל אביב - יפו</p>
</div></div>
I have this code:
WebElement element = driver.findElement(By.cssSelector("#preview-header > div > div > p.address"));
When I debug on run time I see element
is of type RemoteWebElement
and it has strange id 0.01973947323858738-2
and text empty string.
element.getText()
returns ""
element.getAttribute("class")
returns "address"
So it findes the right element, and there is no need for wait
but still I cannot get the text
When I use Chrome inspect element tool
I get an element with text
$("#preview-header > div > div > p.address").text()
What does it mean RemoteWebElement
? can this be the reason I get empty string as getText()
?
- We will see element is of type RemoteWebElement, because "WebElement" interface is implemented in class "RemoteWebElement" and that's fine. Try by introducing some wait before trying to find that element, may be that element is not yet loaded properly. And also make sure css Selector matches only one element on the page – Surya Commented Nov 6, 2014 at 9:38
- it's not related to "wait" as I get the "class" attribute OK. look at my updated questions – Elad Benda Commented Nov 6, 2014 at 10:04
- Okay. good. Can you please try this? Firefox/Chrome: element.getAttribute("textContent") IE: element.getAttribute("innerText") – Surya Commented Nov 6, 2014 at 11:08
3 Answers
Reset to default 6As Surya mentioned in a ment RemoteWebElement
is the class that implements the WebElement
interface. The strange id string you see associated with the RemoteWebElement
object is an id that Selenium uses for its own purposes.
Basically, it's like this:
When you issue one of the
find...
methods, the Selenium client (i.e. your code that calls Selenium methods) tells the Selenium server (which lives in the browser) to perform the search and return an element. The Selenium server searches the DOM for elements and returns them but what it actually returns are not the elements themselves but identifiers that it assigns to the DOM elements. It does this because serializing the actual DOM elements would be very costly, among other problems.On the client side, your code receives these identifiers from the Selenium server. Each identifier is stored in a
RemoteWebElement
object which represents the DOM element.When you execute methods like
getAttribute
on aRemoteWebElement
, Selenium on the client side uses the identifier it has stored and passes it to a mand in the wire protocol to ask the server to perform an operation on the element that has this identifier.
The find...
methods actually have no problem finding elements that the user cannot interact with. (I do it all the time.) However, if you try to interact with those elements with Selenium mands (like using click()
), Selenium will plain that the element cannot be interacted with. Typically this means you have to do something to make the element visible or have to wait for some condition, or something else.
Selenium is designed to mimic a user. Selenium will not interactw ith elements which are not visible to the user. This is by design. You will need to perform whatever action is necessary for the element to be visible to a user before Selenium will interact with it.
The problem was that my element was in the DOM but not visible to the user.
I don't know why, but selenium web drive could find it only when I pressed a tab that revealed this element to the user.
That's weird because I know the element exist even before pressing this tab, because I could find it with the inspect element tool
.