简体   繁体   English

Python Selenium Chrome无法无头运行

[英]Python Selenium Chrome Not Running Headless

I'm currently writing a program in which I'm connecting to chrome. 我目前正在编写要连接到chrome的程序。 I would like to make this process without a new chrome window popping up every time. 我想进行此过程,而不会每次都弹出一个新的Chrome窗口。 I know that --headless makes chrome run silently, but my current code is not working; 我知道--headless使chrome静默运行,但是我当前的代码无法正常工作; it is still opening up chrome tabs. 它仍在打开chrome标签。

Current Code: 当前代码:

chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options = chrome_options)

What am I doing wrong or what do I need to add? 我在做什么错或需要添加什么?

If you are using linux one alternate solution to run the browser in headless mode is to use pyvirtual display. 如果您使用的是Linux,则另一种以无头模式运行浏览器的解决方案是使用pyvirtual显示。 First install xvfb and pyvirtual display: 首先安装xvfb和pyvirtual display:

sudo apt-get install xvfb xserver-xephyr

sudo pip install pyvirtualdisplay

The code will be: 该代码将是:

from pyvirtualdisplay import Display
import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Chrome()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM