9

I'm working in selenium with Firefox browser.

The Html code shown in View Source (CTRL+U) is different from the html code i see when inspecting the elements in Firefox.

When i run the driver.getPageSource() i only get the View source (CTRL + U) codes.

Is there is any way to access the Inspect element code instead of View source code?

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
user2782522
  • 139
  • 1
  • 3
  • 11
  • 1
    possible duplicate of [Get HTML Source of WebElement in Selenium WebDriver (Python)](http://stackoverflow.com/questions/7263824/get-html-source-of-webelement-in-selenium-webdriver-python) – alecxe Oct 10 '14 at 19:33

2 Answers2

7

I think your question is answered here.

The View Source html is what is sent by the server. I think of it as compile time html, or the initial state of the DOM.

The Inspect Element html could have been updated by ajax responses or javascript so will not necessarily be the same. I think of it as runtime html, or the current state of the DOM.

The GetAttribute() method queries the current DOM element state. You can return a particular html attribute value directly

webElement.GetAttribute("class")

or get the whole html string.

webElement.GetAttribute("innerHTML")
Community
  • 1
  • 1
John O.
  • 688
  • 6
  • 9
  • What if `webElement` is dynamically generated and in the DOM (inspect element) but not the source? How can we get to the element itself? – user1002119 Oct 11 '14 at 06:16
  • 1
    That should make no difference. The GetAttribute() method works with whatever is in the DOM. The HTML is the language that describes the DOM, so current HTML is tells you what the current DOM is. I edited the answer to reflect this. – John O. Oct 15 '14 at 21:33
  • 2
    I'm also having the same problem, the page only appears the simple one. the advanced code I can only see in the inspector. Has anyone managed to solve this problem? – BREI May 19 '18 at 22:32
0

There are some fundamental difference between the markup shown through View Source i.e. using ctrl + U and the markup shown through Inspector i.e. using ctrl + shift + I.

Both the methods are two different browser features which allows users to look at the HTML of the webpage. However, the main difference is the View Source shows the HTML that was delivered from the web server (application server) to the browser. Where as, Inspect element is a Developer Tool e.g. Chrome DevTools to look at the state of the DOM Tree after the browser has applied its error correction and after any Javascript have manipulated the DOM. Some of those activities may include:

  • HTML error correction by the browser
  • HTML normalization by the browser
  • DOM manipulation by Javascript

In short, using View Source you will observe the Javascript but not the HTML. The HTML errors may get corrected in the Inspect Elements tool. As an example:

  • With in View Source you may observe:

    <h1>The title</h2>
    
  • Whereas through Inspect Element that would have corrected as:

    <h1>The title</h1>
    

getPageSource() always returns the markup obtained through View Source.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217