Java 如何在 Selenium 中获取 WebElement 的 HTML 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32234205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:11:30  来源:igfitidea点击:

How to get HTML code of a WebElement in Selenium

javaseleniumtestingselenium-webdriverselenium-chromedriver

提问by LoveLovelyJava

I am new at testing so my apologies in advance if my question sounds a bit primary.

我是测试新手,所以如果我的问题听起来有点初级,我提前道歉。

I am using Selenium and Java to write a test.

我正在使用 Selenium 和 Java 来编写测试。

I know that webElement.getAttribute("innerHTML");brings me the innerHTML, for example for the element below:

我知道这 webElement.getAttribute("innerHTML");给我带来了innerHTML,例如下面的元素:

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

it returns:

它返回:

<span class="ui-icon ui-icon-closethick">close</span>

but I need something that brings me the inner attribute of WebElement "a", something like below:

但我需要一些能给我带来 WebElement "a" 内部属性的东西,如下所示:

href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button" style="position: absolute; border-radius: 0px 0px 4px 4px;"

回答by tdrury

webElement.getAttribute("href");
webElement.getAttribute("class");
.
.
.

or to get them all:

或全部获取:

Object[] attr = ((JavascriptExecutor)seleniumdriver).executeScript("return arguments[0].attributes);", webElement);

回答by JeffC

If you want the HTML of the element itself, you can use

如果你想要元素本身的 HTML,你可以使用

webElement.getAttribute("outerHTML");

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.

它将返回元素本身的 HTML 加上所有子元素。我不确定这是否正是你想要的。我认为没有办法只获取所选元素的 HTML。

回答by fahad

Try .getAttribute("innerHTML");function to extract source in string form

尝试.getAttribute("innerHTML");函数以字符串形式提取源

Example code:

示例代码:

String source = driver.findElement(By.xpath("/html/body/script[6]")).getAttribute("innerHTML");

回答by LoveLovelyJava

If we have this:

如果我们有这个:

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

and we need to get all attributes of "a" which will be this:

我们需要获取“a”的所有属性,即:

href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button"
    style="position: absolute; border-radius: 0px 0px 4px 4px;"

We can use this code:

我们可以使用这个代码:

webElement.getAttribute("outerHTML").split(">")[0]

where webElement is "a".

其中 webElement 是“a”。

Or more precisely:

或者更准确地说:

String s = we.getAttribute("outerHTML");

s = s.substring(2, s.indexOf(">"));

回答by Shubham Jain

You can read innerHTML attribute to get source of the content of the element or outerHTML for source with the current element.

您可以读取innerHTML 属性以获取元素内容的来源,或者使用当前元素获取outerHTML 的内容来源。

Example :- Now suppose your Element is as below

示例:- 现在假设您的元素如下

<tr id="myRow"><td>A</td><td>B</td></tr>

Inner element Output

内部元素输出

<td>A</td><td>B</td>

Outer element Output

外部元素输出

<tr id="myRow"><td>A</td><td>B</td></tr>

Live Example :-

现场示例:-

http://www.java2s.com/Tutorials/JavascriptDemo/f/find_out_the_difference_between_innerhtml_and_outerhtml_in_javascript_example.htm

http://www.java2s.com/Tutorials/JavascriptDemo/f/find_out_the_difference_between_innerhtml_and_outerhtml_in_javascript_example.htm

Below you will find the syntax which require as per different binding. Change the innerHTMLto outerHTMLas per required.

您将在下面找到根据不同绑定所需的语法。改变innerHTMLouterHTML按要求。

Python:

Python:

element.get_attribute('innerHTML')

Java:

爪哇:

elem.getAttribute("innerHTML");

C#:

C#:

element.GetAttribute("innerHTML");

Ruby:

红宝石:

element.attribute("innerHTML")

JS:

JS:

element.getAttribute('innerHTML');

If you want whole page HTML use below code :-

如果你想要整个页面的 HTML 使用下面的代码:-

driver.getPageSource();

回答by Usman Kokab

I usually get a source of the element in my test cases to assert. See the following code in which I am testing whether or not it is a single-option dropdown.

我通常会在我的测试用例中获取要断言的元素的来源。请参阅以下代码,我在其中测试它是否为单选项下拉列表。

In the following code, first I got the outer source code of the element and checked whether or not it is having "Multiple" keyword in it.

在下面的代码中,我首先获取元素的外部源代码并检查它是否包含“Multiple”关键字。

   @FindBy(xpath="//div[@class='col-lg-10']//div[1]//div[1]//div[2]//select[1]")
   WebElement element;
   Boolean status = element.getAttribute("outerHTML").contains("Multiple");
   Assert.assertTrue(!status, "User can select only one value at a time in drop down field.");

In my case, it asserted that there is no "Multiple" keyword in outer HTML of the select tag and dropdown is a single-selection dropdown.

就我而言,它断言 select 标签的外部 HTML 中没有“Multiple”关键字,并且下拉列表是单选下拉列表。