Get document height $ (document) .height () using python

I want to get the height of the document for different urls, basically this is supposedly the jQuery equivalent $(document).height()

for all pages. How should I do it?

I'm comfortable using Python and JavaScript.

+3


source to share


1 answer


If you want the size of your browser window, you can use.

get_window_size (windowHandle = 'current')

Returns the width and height of the current window.

Usage: driver.get_window_size ()

But this is not the same as what $(document).height()

you asked for, so the only way to do it is to run the exact same JavaScript command with execute_script

.



from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get("http://google.com")
driver.maximize_window()
height = driver.execute_script("return document.body.scrollHeight")
print height

      

Note. If you want to execute jQuery command you will need to do below.

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://google.com")
driver.maximize_window()
with open('jquery-1.9.1.min.js', 'r') as jquery_js: 
    jquery = jquery_js.read() #read the jquery from a file
    driver.execute_script(jquery) #active the jquery lib
    height = driver.execute_script("return $(document).height()")
    print height

      

+4


source







All Articles