最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Selenium finds remoteWebElement instead of just webElement? - Stack Overflow

programmeradmin1浏览0评论

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()?

Share Improve this question edited Nov 6, 2014 at 10:03 Elad Benda asked Nov 6, 2014 at 8:31 Elad BendaElad Benda 36.7k92 gold badges283 silver badges493 bronze badges 3
  • 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
Add a ment  | 

3 Answers 3

Reset to default 6

As 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:

  1. 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.

  2. 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.

  3. When you execute methods like getAttribute on a RemoteWebElement, 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.

发布评论

评论列表(0)

  1. 暂无评论