selenium获取html源代码

时间:2021-05-18 04:41:17
  • # 执行js得到整个HTML

    html = driver.execute_script("return document.documentElement.outerHTML")

  • 获得整个文档的HTML

    html = driver.find_element_by_xpath("//*").get_attribute("outerHTML")
    # 不要用 driver.page_source,那样得到的页面源码不标准

  • 获取单个元素具体的HTML源文件

    webElement.getAttribute("outerHTML")

  • 获取元素的所有属性
Object[] attr = ((JavascriptExecutor)seleniumdriver).executeScript("return arguments[0].attributes);", webElement);

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:

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

Or more precisely:

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

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