如何在Selenium中获取WebElement的HTML代码 [英] How to get HTML code of a WebElement in Selenium
问题描述
我是测试的新手,所以如果我的问题听起来有些重要,我会提前道歉。
I am new at testing so my apologies in advance if my question sounds a bit primary.
我正在使用Selenium和Java来编写测试。
I am using Selenium and Java to write a test.
我知道
webElement.getAttribute(innerHTML);
给我带来了innerHTML,以下元素的示例:
I know that
webElement.getAttribute("innerHTML");
brings me the innerHTML, for example for the element below:
<a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button" style="position: absolute; border-radius: 0px 0px 4px 4px;">
<span class="ui-icon ui-icon-closethick">close</span>
</a>
它返回:
<span class="ui-icon ui-icon-closethick">close</span>
但我需要一些东西给我带来WebElementa的内部属性,如下所示:
but I need something that brings me the inner attribute of WebElement "a", something like below:
href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button" style="position: absolute; border-radius: 0px 0px 4px 4px;"
推荐答案
如果你想要元素本身的HTML,你可以使用
If you want the HTML of the element itself, you can use
webElement.getAttribute("outerHTML");
它将返回元素本身的HTML以及所有子元素。我不确定这是不是你想要的。我认为没有办法只获取所选元素的HTML。
It will return the HTML of the element itself plus all the children elements. I'm not sure if that's exactly what you want. I don't think there is a way to just get the HTML of the selected element only.
这篇关于如何在Selenium中获取WebElement的HTML代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!