Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blank pages: Selenium Chrome automation in Python

I am trying to automate a process using selenium and chrome browser in Python. My browser works correctly for most pages but is unable to render a few pages including chrome://version/.

For general automation(without headless) it returns the page and page source correctly, whereas for headless browsing it returns a blank page with page source as below

<html><head></head><body></body></html>

I have tried chrome in the different operating system including OpenSUSE, fedora, and Windows. I have tried many things like: Removing all the initial arguments, Used the browser with headless off.

For Reference: If I run this code

from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptions
options = ChromeOptions()
# to remove all arguments
options.add_experimental_option( 'excludeSwitches', ['disable-hang-monitor', 'disable-prompt-on-repost', 'disable-background-networking', 'disable-sync', 'disable-translate', 'disable-web-resources', 'disable-client-side-phishing-detection', 'disable-component-update', 'disable-default-apps', 'disable-zero-browsers-open-for-tests', '--enable-automation', '--use-mock-keychain', '--user-data-dir', '--enable-blink-features', '--disable-popup-blocking', '--enable-logging --force-fieldtrials=SiteIsolationExtensions/Control', '--enable-logging', '--force-fieldtrials', '--ignore-certificate-errors', '--load-extension', '--log-level', '--no-first-run','--password-store','--remote-debugging-port','--test-type'
]) 
options.add_argument("--headless")
options.add_argument("--no-sandbox")
browser = Chrome(executable_path=driver_path,options=options)
browser.get("chrome://version")
print(browser.page_source)

It returns the same blank page for headless

<html><head></head><body></body></html>

If the chrome is operated without headless option it will work completely fine.

<!doctype html>
<!--
about:version template page
-->
<html id="t" dir="ltr" lang="en">
  <head>
    <meta charset="utf-8">
    <title>About Version</title>
    <link rel="stylesheet" href="chrome://resources/css/text_defaults.css">
    <link rel="stylesheet" href="chrome://version/about_version.css">
......................
like image 788
Mayank Avatar asked Nov 07 '22 13:11

Mayank


1 Answers

I guess that selenium is starting browser in headless and, because it's getting chrome://version from local files, it just can't render it.

I think that the same will happen with all chrome:// (and local files as .pdf or something) if running in headless.

Try opening some pdf file in chrome browser and then: driver.get(<path_to_pdf>) both headless and normal

like image 70
Misieq Avatar answered Nov 11 '22 19:11

Misieq