Get page generated with Javascript in Python
32,376
You could use Selenium Webdriver:
#!/usr/bin/env python
from contextlib import closing
from selenium.webdriver import Firefox # pip install selenium
from selenium.webdriver.support.ui import WebDriverWait
# use firefox to get page with javascript generated content
with closing(Firefox()) as browser:
browser.get(url)
button = browser.find_element_by_name('button')
button.click()
# wait for the page to load
WebDriverWait(browser, timeout=10).until(
lambda x: x.find_element_by_id('someId_that_must_be_on_new_page'))
# store it to string variable
page_source = browser.page_source
print(page_source)
Related videos on Youtube
Author by
xralf
Updated on January 23, 2020Comments
-
xralf over 4 years
I'd like to download web page generated by
Javascript
and store it to string variable inPython
code. The page is generated when you click on button.If I would know the resulting URL I would use
urllib2
but this is not the case.thank you
-
e-satis over 12 yearsIs this generated completly in js or just built from an ajax call ?
-
xralf over 12 years@e-satis I think that it's completely in js
-
e-satis over 12 yearsThen I'd got with J.F solution, or with python webkit. Just keep in mind they require a display server to be running so if you plan to make it run on a headless server, you'll need to hack a little bit.
-
-
xralf over 12 yearsis the
WebDriverWait
withsomeId_that_must_be_on_new_page
neccessary? Could it be done only with somesleep
ordelay
function? And is it possible to set the user-agent string? -
xralf over 12 yearsThere is one problem yet. On the web page is
select
element and something have to be selected. If nothing is selected the button won't work. And is neccessary to open and close firefox? Withoutguit
this won't work? -
jfs over 12 yearsyou could use any condition you like e.g.,
x.title == 'New Title'
. You probably could modify user-agent by using appropriate firefox profile. -
jfs over 12 yearshere's an example on how to select option.
.quit()
is not necessary. -
xralf over 12 yearsThe method
select_option(self, selector, value)
takesselector
parameter. I'm not sure what this parameter should be. Let's say I want to click on option withvalue = 100
ofselect
withid = 'sel_id'
andname = 'sel_name'
. Could this be expressed inCSS
? -
jfs over 12 years@xralf:
select_option('select#sel_id', '100')
. You could pass an element insteadselect_option(browser.find_element_by_id('sel_id'), '100')
. -
xralf over 12 yearsThanks. I already used
options = browser.find_elements_by_tag_name('option') for option in options: if option.get_attribute('value') == "100": option.click()
and worked too. -
alper over 3 yearsCan this done by opening Firefox window on the background?
-
jfs over 2 years@alper yes, there headless options