Python Selenium:如何使浏览器 window 打不开

Python Selenium: How to make browser window not open

如何让selenium window打不开?我不想看到打开 window,但我需要使用 selenium,因为我需要获取有关 headers 的信息,它们加载速度非常慢。 代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time 
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

PATH =".\msedgedriver.exe"
driver = webdriver.Edge(PATH)

driver.get("https://www.verivox.de/stromvergleich/vergleich/#/?plz=10555&persons=on&usage=3500&bonus=OnlyCompliant&profile=H0&product=electricity&source=1&q=WzYsMCwxLDEsMSwxLDEsMiwyMCwwLDEsNzQxMTIyLCI3MTA4NSIsMSwyNDAsMjQwLDM1MDAsMCwwLDAsOTk5LC0xLC0xLC0xLDAsMCwiVG90YWxDb3N0cyIsIkFzY2VuZGluZyIsIk5vbmUiLDM4LCJBbm51YWxDb3N0VmlldyIsMF0%3D&partnerid=1")


allheader=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"li[class='result-item'] .result-name-area>.result-name")))
for header in allheader:
     print("Header: " + header.text)

我尝试使用 phantonJS() 但我的代码无法正常工作并得到

UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
  warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '

我也尝试使用 xvfbwrapper,但出现错误

Traceback (most recent call last):
  File "skriptas.py", line 8, in <module>
    from xvfbwrapper import Xvfb
  File "C:\Users\andri\AppData\Roaming\Python\Python38\site-packages\xvfbwrapper.py", line 15, in <module>
    import fcntl
ModuleNotFoundError: No module named 'fcntl'

知道怎么做吗?谢谢你的帮助。

这条消息...

UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
  warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '

...不是这样的错误,而是 支持的弃用警告。


解决方案

使用以下代码行来抑制错误:

import warnings

warnings.filterwarnings("ignore")