java如何dom获取,如何使用Selenium Java 2.8获取当前的DOM?

博主使用Selenium和chromedriver测试ZK应用,想获取DOM辅助查找元素。WebDriver.getPageSource()只能返回服务器发送的HTML,WebElement.getText()仅为元素文本。最终找到通过ExecuteScript获取DOM的方法,如GetElementParent函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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?

解决方案

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

///

/// Gets the parentElement/Node of a particular element

///

///

///

///

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值