You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importasyncioimportosimportplatformimportshutilfromselenium_driverlessimportwebdriverasyncdefmain():
options=webdriver.ChromeOptions()
driver=Noneifplatform.system() =="Windows":
possible_paths= [
os.path.expandvars(r"%ProgramFiles%\Google\Chrome\Application\chrome.exe"),
os.path.expandvars(r"%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"),
os.path.expandvars(r"%LocalAppData%\Google\Chrome\Application\chrome.exe")
]
chrome_path=next((pathforpathinpossible_pathsifos.path.exists(path)), None)
else:
chrome_path=shutil.which("google-chrome")
ifchrome_pathisNone:
print("Error: Could not find Google Chrome binary.")
returnprint(f"Using Chrome binary at: {chrome_path}")
options.binary_location=chrome_pathtry:
driver=awaitwebdriver.Chrome(options=options)
print("WebDriver initialized.")
# Get absolute path to example.htmlhtml_file_path=os.path.abspath("example.html")
# Format for file URLfile_url=f"file:///{html_file_path.replace(os.sep, '/')}"print(f"Navigating to: {file_url}")
awaitdriver.get(file_url)
print("Navigation complete.")
# Wait a moment for scripts to potentially execute (optional)awaitasyncio.sleep(1)
print("Executing script to get window.__INITIAL_DATA__...")
script="return typeof window.__INITIAL_DATA__ !== 'undefined' ? window.__INITIAL_DATA__ : 'Not Defined';"initial_data=awaitdriver.execute_script(script)
print("\n--- Extracted Data ---")
print(initial_data)
print("----------------------")
exceptExceptionase:
print(f"\nAn error occurred: {e}")
try:
# Attempt to get page source on error for debuggingsource=awaitdriver.page_sourceprint("\n--- Page Source on Error ---")
print(source[:1000] +"...") # Print first 1000 charsprint("--------------------------")
exceptExceptionaspe:
print(f"Could not get page source after error: {pe}")
finally:
ifdriver:
print("\nClosing WebDriver...")
awaitdriver.quit()
print("WebDriver closed.")
if__name__=="__main__":
asyncio.run(main())
Html File
<!DOCTYPE html><html><head><title>Test Page</title><scripttype="text/javascript">window.__INITIAL_DATA__={"user": "testUser","sessionId": 12345,"settings": {"theme": "dark","notifications": true}};console.log("window.__INITIAL_DATA__ defined:",window.__INITIAL_DATA__);</script></head><body><h1>Test Page for window.__INITIAL_DATA__</h1><p>Check the console and the script output.</p></body></html>
Output
Using Chrome binary at: C:\Program Files\Google\Chrome\Application\chrome.exe
WebDriver initialized.
Navigating to: file:///C:/Users/sweed/Downloads/initial_data_bug/example.html
Navigation complete.
Executing script to get window.__INITIAL_DATA__...
c:\Python\Lib\site-packages\selenium_driverless\types\deserialize.py:175: UserWarning: got execution_context_id and unique_context=True, defaulting to execution_context_id
warnings.warn("got execution_context_id and unique_context=True, defaulting to execution_context_id")
--- Extracted Data ---
Not Defined
----------------------
Closing WebDriver...
WebDriver closed.
The text was updated successfully, but these errors were encountered:
This was solved with unique_context=False, but confirm that this is expected behaviour to not work when no value is provided for unique_context. The warning says "got execution_context_id and unique_context=True, defaulting to execution_context_id" which makes me think it should ignore unique_context.
Even though the example html defines it, the executed script doesn't find it.
https://github.com/biegehydra/initial_data_bug
Python File
Html File
Output
The text was updated successfully, but these errors were encountered: