Python Selenium Headless Chrome [duplicado]
Estoy tratando de hacer que funcione el cromo sin cabeza y simplemente no puedo hacerlo funcionar. Creé un archivo de prueba que funciona:
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)
funciona pero también produce un error:
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)
Puedo vivir con el error, supongo, porque se abre sin cabeza, obtiene Chrome, imprime el título como pedí, pero cuando voy a pegar esto en mi proyecto principal, todavía abre una ventana, la ventana está en blanco y simplemente se sienta allí como una ventana en blanco hasta que termine de ejecutarse. ¿Alguna idea de qué está pasando? Aquí está el comienzo de mi código de proyecto:
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()
He intentado varias formas diferentes de hacer esto, pero solo la anterior funciona un poco:
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)
y
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)
Respuestas
driver = webdriver.Chrome(options=options)
el error dice correctamente lo que significa, use options = your_option
quechrome_options = your_option
Lo más probable es que haya una ventana vacía porque está llamando al "controlador" dos veces, pero la primera no tiene URL, por lo que está en blanco. Me desharía de la primera variable del controlador.