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



如何使硒窗口不打开?我不想看到打开的窗口,但我需要使用selenium,因为我需要获得关于标头的信息,这些标头加载非常慢。代码:

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:UsersandriAppDataRoamingPythonPython38site-packagesxvfbwrapper.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 '

这并不是一个错误,而是对phantomjs支持的弃用警告。


解决方案

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

import warnings
warnings.filterwarnings("ignore")

相关内容

  • 没有找到相关文章

最新更新