PythonSeleniumヘッドレスクローム[複製]

Dec 11 2020

ヘッドレスクロームを機能させようとしていますが、機能させることができません。私は動作するテストファイルを作成しました:

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)

ヘッドレスで開き、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()

私はこれを行うために複数の異なる方法を試しましたが、上記の1つだけがちょっとうまくいきます:

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)

そして

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

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

回答

2 PDHide Dec 11 2020 at 09:54
driver = webdriver.Chrome(options=options)

エラーを正しく使用、それが何を意味するのかと言うoptions = your_optionよりは、chrome_options = your_option

1 dachil1pil1 Dec 11 2020 at 11:34

「ドライバ」を2回呼び出しているため、空のウィンドウが表示される可能性がありますが、最初のウィンドウにはURLがないため、空白になっています。私は最初のドライバー変数を取り除きます。