简体   繁体   English

如何在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. 我是测试的新手,所以如果我的问题听起来有些重要,我会提前道歉。

I am using Selenium and Java to write a test. 我正在使用Selenium和Java来编写测试。

I know that webElement.getAttribute("innerHTML"); 我知道webElement.getAttribute("innerHTML"); brings me the innerHTML, for example for the element below: 给我带来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;"

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. 它将返回元素本身的HTML以及所有子元素。 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。

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 innerHTML to outerHTML as per required. 根据需要将innerHTML更改为outerHTML

Python: 蟒蛇:

element.get_attribute('innerHTML')

Java: 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();
webElement.getAttribute("href");
webElement.getAttribute("class");
.
.
.

or to get them all: 或者让他们全部:

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

Try .getAttribute("innerHTML"); 试试.getAttribute("innerHTML"); function to extract source in string form 函数以字符串形式提取源

Example code: 示例代码:

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

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(">"));

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”关键字,下拉列表是单选下拉列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM