Christian
Christian

Reputation: 6430

How do I access Selenium WebDriver's (DOM) representation of a page in a debugger?

I just joined a team who use Selenium WebDriver (with Java), and I was wondering if it holds an object representation (e.g. a DOM model) of the current page in memory that I can look at in a debugger?

At the moment, I get a lot of errors that the webdriver can't find elements I'm looking for, and I thought it might help being able to "click through" to see what (elements) it actually knows about. However, the webdriver-related instances I examined didn't seem to hold any page-specific information.

Surely, webdriver has some sort of representation of a page's content in memory? What class' instance am I looking for?

Upvotes: 1

Views: 1648

Answers (1)

Louis
Louis

Reputation: 151401

Solution

End your Selenium script without formally closing the browser (don't call quit() or methods that do something like quit()). This way the browser remains around and you can open the developer tools to inspect the DOM.

Explanation

Selenium does not have a representation of the DOM on the client side. The client side is your Selenium script.

Surely, webdriver has some sort of representation of a page's content in memory?

No.

What class' instance am I looking for?

There is none, for what you are doing.

The way WebDriver works is by sending wire commands to a small server that lives inside the browser you are testing. The WebDriver client does not keep information about the page that the browser is showing but queries the server as needed when you issue your Selenium commands. It can remember the identity of elements it finds for you because when you search for an element in the page and the server returns it, it returns a string that uniquely identifies the element, and this string is wrapped into a WebElement object. (By the way, this is why you can get a StaleElementReferenceException. If the element that was returned earlier is later removed by some JavaScript code in the browser, then your reference refers to an element that is no longer there!) Then when you use the methods and fields of the object, Selenium uses this unique identifier to tell the server what element you are referring to.

Having a representation of the DOM on the client side would be a) costly, b) awkward, b) limited, d) all of the above.

So, what do you do? Use the developer tool in the browser. I've found that Selenium just does not like you opening these tools while it is in control. It's been a while since I've tried but when I tried Selenium could automatically close the developer tools after I open them. I would open them, and it would close them, I open, it closes, I open, it closes. Ending your script without telling Selenium to close the browser works around this.

Upvotes: 3

Related Questions