-1

How to print all html/css tags of a webpage using Selenium:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/')

When I do:

print(browser),

it prints this:

"<selenium.webdriver.firefox.webdriver.WebDriver (session="ce01359c-03e4-499d-a3fb-230bda9ac24c")>"

Is this an Object or variable or a list/set/tuple/dict or what is it? Could someone explain it please?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user3013157
  • 109
  • 1
  • 9
  • 1
    It's an object (as is *everything else in Python*). You chose to assign it to the variable named `browser`. Specifically, the object is an instance of some class defined within the `selenium` package; it exists for the primary purpose of invoking documented methods such as `.get()` on it, printing out the object itself is not something that would normally be done. – jasonharper Aug 01 '19 at 01:51
  • In the code, browser holds Firefox driver object returned by webdriver.Firefox(). Calling browser.get() does not change 'browser' in any way. So when you print 'browser' it will print the object. What exactly were you expecting it to print? What was your purpose of printing it? – Rohit Aug 01 '19 at 04:56
  • @jasonharper thanks, very clear. Is there a way to print all content of "browser"? Not just look for something specific using browser.find_elements_by...(...)? – user3013157 Aug 05 '19 at 08:22

3 Answers3

3

The result you're getting is an object. In the beginning of your code you chose to call this object browser (a.k.a assigning it to a variable). When you then run the function browser.get() it will not change the content of the variable browser and therefore the result will have nothing to do with the webpage that you're on.

Mr. Blue
  • 200
  • 2
  • 2
  • 13
0

You should take a look at the documentation, specifically this page as it gives a good introduction. If you work through that explanation you should get a good (basic) understanding of how the API works. Additional chapters can fill in the gaps about the specifics of locating elements within a page.

On the other hand, if all you're doing is scraping HTML may I suggest you take a look at Beautiful Soup.

smilechaser
  • 376
  • 2
  • 5
0

You saw it right. As per the documentation of New Session, the New Session command creates a new WebDriver session with the endpoint node.

HTTP Method     URI Template
POST            /session

As per the remote end steps, the entire process entirely depends up to the implementation , but typically the sessionId, and URL and URL prefix of the upstream remote end are needed to be tracked. Additionally,

  • The session id is the result of generating a UUID.

  • The session be a new session with the session ID of session id.

Hence, browser which is an object of class selenium.webdriver.firefox.webdriver.WebDriver() prints the following:

"<selenium.webdriver.firefox.webdriver.WebDriver (session="ce01359c-03e4-499d-a3fb-230bda9ac24c")>"

You can find a revelant discussion in Values returned by webdrivers

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352