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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM