Search code examples
pythonseleniumghostdriver

Need to dump entire DOM tree with element id from selenium server


I have been using python selenium for web automation testing. The key part of automation is to find the right element for a user-visible object in a HTML page. The following API will work most of the time, but not all the time.

find_element_by_xxx,  xxx can be id, name, xpath, tag_name etc. 

When HTML page is too complicated, I would like to search the dom tree. Wonder if it's possible to ask the selenium server to serialize the entire DOM (with the element id that can be used to perform action on through webdriver server). Client side (python script) can do its own search algorithm to find the right element.

Note that python selenium can get the entire html page by

drv.page_source

However, parsing this doesn't give the internal element id from selenium server's point of view, hence not useful.

EDIT1: Paraphrase it to make it more clear (thanks @alecxe): what's needed here is a serialized representation of all the DOM elements (with their DOM structure preserved) in the selenium server, this serialized representation can be sent to the client side (a python selenium test app) which can do its own search.


Solution

  • Try:

    find_elements_by_xpath("//*")
    

    That should match all elements in the document.

    UPDATE (to match question refinements):

    Use javascript and return the DOM as a string:

    execute_script("return document.documentElement.outerHTML")