How do I get the current DOM with Selenium Java 2.8?

随声附和 提交于 2019-12-22 05:00:37

问题


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



来源:https://stackoverflow.com/questions/7712771/how-do-i-get-the-current-dom-with-selenium-java-2-8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!