【发布时间】:2020-01-24 07:54:06
【问题描述】:
我正在尝试在 Python 中使用 selenium 和 chrome 浏览器自动化一个过程。
我的浏览器在大多数页面上都能正常工作,但无法呈现包括chrome://version/ 在内的一些页面。
对于一般自动化(无头),它会正确返回页面和页面源,而对于 headless 浏览,它会返回一个空白页面,页面源如下
<html><head></head><body></body></html>
我在不同的操作系统中尝试过 chrome,包括 OpenSUSE、fedora 和 Windows。 我尝试了很多事情,例如: 删除所有初始参数, 使用无头浏览器。
供参考: 如果我运行这段代码
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)
它为 headless
返回相同的空白页<html><head></head><body></body></html>
如果chrome在没有headless选项的情况下运行,它将完全正常工作。
<!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">
......................
【问题讨论】:
-
@DebanjanB 重复的问题与这个问题没有任何共同之处。
-
@AndiCover 在 OP 编辑之前观察initial version of the question 中的错误
(unknown error: DevToolsActivePort file doesn't exist)。 -
@DebanjanB 该错误是我尝试并提到的一种方法,以防万一。这与原始问题无关,与问题陈述完全无关。请考虑删除重复的标签。
-
@DebanjanB 我做到了,甚至在发布这个问题之前我已经经历了这个。我的问题是“一些页面无法正确加载并返回空白页面”与“无法启动浏览器”的问题重复。请了解问题及其用例,并在可能的情况下考虑提供可能的解决方案。
-
您可以标记问题以引起版主的注意。如果这没有帮助,我建议删除问题并再次询问。
标签: javascript python selenium google-chrome selenium-webdriver