Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to get document height for various url, basically it's suppose to be the jQuery equivalent of $(document).height() for all pages. How should I go about this?

I am comfortable using Python and JavaScript.

like image 270
Taranjeet Singh Avatar asked Mar 08 '23 12:03

Taranjeet Singh


1 Answers

If you want browser's window size then you can use.

get_window_size(windowHandle='current')

Gets the width and height of the current window.

Usage: driver.get_window_size()

But that is not same as $(document).height(), which you have asked for, so the only way to do it is to trigger the same JavaScript command using 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 the jQuery command, then you'll have 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
like image 142
Chankey Pathak Avatar answered Mar 20 '23 11:03

Chankey Pathak