Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch console log of an iframe with Python, Selenium and Chrome?

The project is a web page with another app embedded as an iframe and I'm trying to write a test to check that no errors are logged in the embedded app. The issue is that get_log doesn't return the logs of the embedded app

log = driver.get_log('browser')
logger.info(log) # only contains the main page logs, and iframe logs are skipped
capabilities['goog:loggingPrefs'] = {'browser': 'ALL'}

is used when launching chrome.

Also tried with and without this before get_log but it makes no difference, even though driver.page_source does change correctly, and elements inside the iframe can be accessed normally.

driver.switch_to.frame(frame_id)

Still only the console log of the parent of the iframe are returned by get_log, but I also need the iframe logs, which are correctly shown in devtools in the chrome browser instance opened by selenium.

Any ideas or workarounds on how to load all of the logs into the python test?

Selenium 4.4.3

Python 3.9.2

Chrome (and chromedriver) 107.0.5304.87

like image 993
Reksa Avatar asked Dec 21 '25 21:12

Reksa


1 Answers

Maybe a bit too late, but I've found the solution. First of all, here is our problem: https://bugs.chromium.org/p/chromium/issues/detail?id=924937

Secondly, as a temporary solution, we may add this parameters to Chrome:

options.add_argument("--disable-features=IsolateOrigins,site-per-process")
options.add_argument("--disable-site-isolation-trials")

Briefly, problem appeared in Google Chrome when iframes started working in separate processes. These options return old behaviour as they worked in the same process as main page. In my first reference also described another workaround in which you can attach to specific iframe and get its data. But I haven't tried it because I'm fully satisfied with old behaviour solution. :)

Hope this'll help someone, cause I've spend all day and half of the night looking for solution. Glad I've found it.

like image 113
Yura Khaltin Avatar answered Dec 24 '25 10:12

Yura Khaltin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!