Python 无头铬

Python 无头铬,python,selenium,google-chrome-headless,Python,Selenium,Google Chrome Headless,我正试图让无头铬的工作,我只是不能让它工作。我创建了一个有效的测试文件: from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument("--headless") driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe" , chrome_options=opti

我正试图让无头铬的工作,我只是不能让它工作。我创建了一个有效的测试文件:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--headless")

driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe" , chrome_options=options)
driver.get('http://www.google.com')
print(driver.title)
它可以工作,但也会产生错误:

C:/Users/kgood/PycharmProjects/pythonProject1/Unknown.py:6: DeprecationWarning: use options instead of chrome_options
  driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe" , chrome_options=options)
我可以接受这个错误,因为它确实打开了headless,获得了Chrome,按照我的要求打印了标题,但是当我把它粘贴到我的主项目中时,它仍然会打开一个窗口,窗口是空白的,它只是作为一个空白窗口放在那里,直到它完成运行。你知道怎么回事吗?以下是我的项目代码的开头:

from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
options = webdriver.ChromeOptions()
options.add_argument("--headless")

# gets the website
driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe" , chrome_options=options)
driver.get("https://www.legacyhomesal.com/pennington-freedom-series-richmond-ii")

# finds the Base Price header
price = driver.find_element_by_xpath("//h3[@class='ng-binding']")
print(price.text)

# converts the string to integer
p = price.text[12::]
r = int(p.replace(',', ''))
driver.close()
我尝试了多种不同的方法,但只有上面的一种有效:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')  # Last I checked this was necessary.
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)

错误正确地说明了它的意思,使用选项=您的选项,而不是chrome\u选项=您的选项


错误正确地说明了它的意思,使用选项=您的选项而不是chrome\u选项=您的选项很可能是一个空窗口,因为您调用驱动程序两次,但第一个没有url,所以它只是空白。我会去掉第一个驱动变量。

很可能是一个空窗口,因为您要调用驱动程序两次,但第一个并没有url,所以它只是空的。我会去掉第一个驱动变量。

那个么你们的问题是什么?换句话说,你被困在哪里?你看到了什么错误?那么你的问题是什么?换句话说,你被困在哪里?你看到了什么错误?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.headless = True
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)
driver = webdriver.Chrome(options=options)