How do I get the current DOM with Selenium Java 2.

2019-04-25 22:39发布

问题:

I'm using the latest version of Selenium and the chromedriver to test a ZK application.

During the test, I'd like to dump the DOM (or part of it) to help me find the elements I need (and probably help people who have to maintain the test).

The method WebDriver.getPageSource() looked promising but it only returns the HTML as it was sent by the server, not the result after running all the JavaScript code.

The JavaScript code is run; I can find elements by ID that I can't see in the output of getPageSource(). So I tried WebElement.getText() but that is only the text of the elements, not the elements themselves or their attributes.

Is it possible at all to get the DOM or do I have to do keyhole surgery here?

回答1:

I have the same question really but the only way I've found to do it is ExecuteScript:

/// <summary>
        /// Gets the parentElement/Node of a particular element
        /// </summary>
        /// <param name="driver"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        public static IWebElement GetElementParent(IWebDriver driver,IWebElement element)
        {
            return (IWebElement) ((IJavaScriptExecutor)driver).ExecuteScript("return arguments[0].parentNode", element);
        }

why the By doesnt support By.DOM with a function I don't really know...I suspect its due to the need for a webdriver for multiple browsers etc