問題描述
我是測試新手,所以如果我的問題聽起來有點初級,我提前道歉.
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>
但我需要一些能夠為我帶來 WebElement "a" 內部屬性的東西,如下所示:
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 代碼的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!