zoukankan      html  css  js  c++  java
  • selenium获取html源代码

    • # 执行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(">"));
     
  • 相关阅读:
    你读了该博客中哪些超链接?有何感想
    最理想的师生关系是健身教练和学员的关系,在这种师生关系中你期望获得来自老师的哪些帮助?
    1500802028 王莉娟
    解码方法
    N皇后问题
    两个链表的交叉
    全排列
    交叉字符串
    翻转链表
    爬楼梯
  • 原文地址:https://www.cnblogs.com/hushaojun/p/5985673.html
Copyright © 2011-2022 走看看