如何使用Selenium Java 2.8获取当前的DOM? [英] How do I get the current DOM with Selenium Java 2.8?

查看:246
本文介绍了如何使用Selenium Java 2.8获取当前的DOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新版本的Selenium和chromedriver来测试ZK应用程序.

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

在测试过程中,我想转储DOM(或部分DOM)以帮助我找到所需的元素(并可能帮助需要维护测试的人员).

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

方法WebDriver.getPageSource()看起来很有希望,但是它只返回服务器发送的HTML,而不是运行所有JavaScript代码后的结果.

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.

JavaScript代码已运行;我可以通过在getPageSource()的输出中看不到的ID查找元素.所以我尝试了WebElement.getText(),但这只是元素的文本,而不是元素本身或它们的属性.

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.

是否有可能获得DOM或我必须在这里进行锁孔手术?

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

推荐答案

我确实有相同的问题,但发现的唯一方法是ExecuteScript:

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);
        }

为什么By不支持不真正具有功能的By.DOM ...我怀疑是由于多个浏览器等需要Webdriver导致的

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

这篇关于如何使用Selenium Java 2.8获取当前的DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆