Get HTML Source of WebElement in Selenium WebDriver using Python

后端 未结 14 1751
误落风尘
误落风尘 2020-11-22 13:45

I\'m using the Python bindings to run Selenium WebDriver:

from selenium import webdriver
wd = webdriver.Firefox()

I know I can grab a webel

相关标签:
14条回答
  • 2020-11-22 14:18

    In Ruby, using selenium-webdriver (2.32.1), there is a page_source method that contains the entire page source.

    0 讨论(0)
  • 2020-11-22 14:18

    InnerHTML will return element inside the selected element and outerHTML will return inside HTML along with the element you have selected

    Example :- Now suppose your Element is as below

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

    innerHTML element Output

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

    outerHTML 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

    Below you will find the syntax which require as per different binding. Change the innerHTML to outerHTML as per required.

    Python:

    element.get_attribute('innerHTML')
    

    Java:

    elem.getAttribute("innerHTML");
    

    If you want whole page HTML use below code :-

    driver.getPageSource();
    
    0 讨论(0)
  • 2020-11-22 14:20

    The method to get the rendered HTML I prefer is following:

    driver.get("http://www.google.com")
    body_html = driver.find_element_by_xpath("/html/body")
    print body_html.text
    

    However the above method removes all the tags( yes the nested tags as well ) and returns only text content. If you interested in getting the HTML markup as well, then use the method below.

    print body_html.getAttribute("innerHTML")
    
    0 讨论(0)
  • 2020-11-22 14:21

    Looks outdated, but let it be here anyway. The correct way to do it in your case:

    elem = wd.find_element_by_css_selector('#my-id')
    html = wd.execute_script("return arguments[0].innerHTML;", elem)
    

    or

    html = elem.get_attribute('innerHTML')
    

    Both are working for me (selenium-server-standalone-2.35.0)

    0 讨论(0)
  • 2020-11-22 14:21

    I hope this could help: http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html

    Here is described Java method:

    java.lang.String    getText() 
    

    But unfortunately it's not available in Python. So you can translate the method names to Python from Java and try another logic using present methods without getting the whole page source...

    E.g.

     my_id = elem[0].get_attribute('my-id')
    
    0 讨论(0)
  • 2020-11-22 14:25

    This works seamlessly for me.

    element.get_attribute('innerHTML')
    
    0 讨论(0)
提交回复
热议问题