Skip to content

Instantly share code, notes, and snippets.

@jrosell
Last active January 20, 2023 18:49
Show Gist options
  • Save jrosell/22447360bcb3824c113dc3548bea4843 to your computer and use it in GitHub Desktop.
Save jrosell/22447360bcb3824c113dc3548bea4843 to your computer and use it in GitHub Desktop.
Crawl a URL and capture network requests to third party resources like Google Analytics.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
def intercept_requests(url, consent_css = "button[id*='onetrust-accept-btn-handler']"):
print("Start ##############################")
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(
service=Service(ChromeDriverManager().install()),
options=chrome_options
)
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': """
window.intercepted_urls = [] || window.intercepted_urls;
let oldXHROpen = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
this.addEventListener('load', function() {
console.log('intercepted '+url+': ' + this.responseText);
window.intercepted_urls.push(url)
console.log(window.intercepted_urls)
});
return oldXHROpen.apply(this, arguments);
}
"""})
driver.get(url)
# wait = WebDriverWait(driver, 5)
# wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[style='bottom: 1rem'] button[id*='onetrust-accept-btn-handler']")))
driver.find_element(By.CSS_SELECTOR, consent_css).click()
time.sleep(15)
urls = driver.execute_script(""" return window.intercepted_urls;""")
driver.quit()
print("Intercepted URLs:")
print(urls)
print("End ##############################")
return urls
@jrosell
Copy link
Author

jrosell commented Jan 17, 2023

Start ##############################
Intercepted URLs:
['https://cookie-cdn.cookiepro.com/consent/b01eb98e-0c14-43c6-bea6-8eea44702da6/b01eb98e-0c14-43c6-bea6-8eea44702da6.json', '//pronovias.tt.omtrdc.net/m2/pronovias/mbox/json?mbox=target-global-mbox&mboxSession=522ee66d10be49af91d440d214df270c&mboxPC=&mboxPage=1866f0d4581c41a3ac0f320231bc05ff&mboxRid=d6c478f777254096b48b8eebdfdeb3ee&mboxVersion=1.8.2&mboxCount=1&mboxTime=1674243953361&mboxHost=www.pronovias.com&mboxURL=https%3A%2F%2Fwww.pronovias.com%2Fes%2F&mboxReferrer=&browserHeight=600&browserWidth=785&browserTimeOffset=60&screenHeight=600&screenWidth=800&colorDepth=24&devicePixelRatio=1&screenOrientation=landscape&webGLRenderer=ANGLE%20(Google%2C%20Vulkan%201.3.0%20(SwiftShader%20Device%20(Subzero)%20(0x0000C0DE))%2C%20SwiftShader%20driver)', 'https://www.pronovias.com/static/version1672217730/frontend/Hiberus/pronovias/es_ES/js-translation.json', 'https://www.pronovias.com/static/version1672217730/frontend/Hiberus/pronovias/es_ES/Hiberus_Geolocation/template/modal/country-selector.html', 'https://www.pronovias.com/static/version1672217730/frontend/Hiberus/pronovias/es_ES/Magento_Ui/templates/modal/modal-popup.html', 'https://www.pronovias.com/static/version1672217730/frontend/Hiberus/pronovias/es_ES/Magento_Ui/templates/modal/modal-slide.html', 'https://www.pronovias.com/static/version1672217730/frontend/Hiberus/pronovias/es_ES/Magento_Ui/templates/modal/modal-custom.html', 'https://www.pronovias.com/static/version1672217730/frontend/Hiberus/pronovias/es_ES/mage/multiselect.html', 'https://www.pronovias.com/static/version1672217730/frontend/Hiberus/pronovias/es_ES/Magento_Ui/templates/tooltip/tooltip.html', 'https://www.pronovias.com/es/banner/ajax/load/?requesting_page_url=https%3A%2F%2Fwww.pronovias.com%2Fes%2F&sections=&_=1674240353406', 'https://www.pronovias.com/static/version1672217730/frontend/Hiberus/pronovias/es_ES/Magento_Ui/templates/collection.html', 'https://www.pronovias.com/static/version1672217730/frontend/Hiberus/pronovias/es_ES/Pronovias_Events/template/events/widget/slider.html', 'https://www.google-analytics.com/j/collect?v=1&_v=j99&a=1753559957&t=pageview&_s=1&dl=https%3A%2F%2Fwww.pronovias.com%2Fes%2F&ul=en-us&de=UTF-8&dt=Inicio%20%7C%20Pronovias%20%7C%20Marca%20internacional%20l%C3%ADder%20en%20moda%20nupcial%20de%20lujo&sd=24-bit&sr=800x600&vp=785x600&je=0&_u=YEBAAEABAAAAACgEK~&jid=451840814&gjid=1901725958&cid=1263270145.1674240355&tid=UA-56968671-1&_gid=1757768388.1674240355&_r=1&_slc=1&gtm=2wg1i0KP4N9LL&cg1=home&cd1=pronovias&cd2=home&cd7=es_es&cd11=production&cd14=inicio%20%7C%20pronovias%20%7C%20marca%20internacional%20l%C3%ADder%20en%20moda%20nupcial%20de%20lujo&cd28=GTM-KP4N9LL&cd29=30&cd30=2023-01-20T19%3A45%3A54.558%2B01%3A00&cd31=a36b344f-92cb-441b-add8-e22fd5e9f434&cd27=pageview&cd32=1263270145.1674240355&z=1516246574', 'https://stats.g.doubleclick.net/j/collect?t=dc&aip=1&_r=3&v=1&_v=j99&tid=UA-56968671-1&cid=1263270145.1674240355&jid=451840814&gjid=1901725958&_gid=1757768388.1674240355&_u=YEBAAEAAAAAAACgEK~&z=1525616482', 'https://ct.pinterest.com/user/?tid=2617296701223&pd=%7B%22np%22%3A%22gtm%22%7D&cb=1674240355402&dep=2%2CPAGE_LOAD', 'https://ct.pinterest.com/user/?event=pagevisit&ed=%7B%22np%22%3A%22gtm%22%7D&tid=2617296701223&cb=1674240355406&dep=5%2CEVENT_TAGS_ABSENT', 'https://bam.eu01.nr-data.net/jserrors/1/NRJS-d830d3c7fa152da5c07?a=36875703&v=1221.PROD&to=MhBSZQoZC0sFUhcLWAtacVIMEQpWS1IOERgMG1RUAFcMVgBUGw%3D%3D&rst=11605&ck=0&s=fef67bd424cc7afb&ref=https://www.pronovias.com/es/', 'https://bam.eu01.nr-data.net/events/1/NRJS-d830d3c7fa152da5c07?a=36875703&v=1221.PROD&to=MhBSZQoZC0sFUhcLWAtacVIMEQpWS1IOERgMG1RUAFcMVgBUGw%3D%3D&rst=11604&ck=0&s=fef67bd424cc7afb&ref=https://www.pronovias.com/es/']
End ##############################

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment