Sorry but I'm quite new to Python. I have checked online and found that there are many videos with ways to create headless google selenium. However, upon trying those methods, I realized that they are all deprecated. Can anyone update me with the latest method to enable headless Selenium for google Chrome?
[Updated] I have tried adding the suggested answer to my code. This is my new code:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
browser_options = Options()
browser_options.headless = True
browser = webdriver.Chrome(executable_path=r'C:\Users\Jonathan\Projects\drivers\chromedriver_win32_V96\chromedriver.exe',options=browser_options)
browser.get("https://info.bbdc.sg/members-login/")
login_refresh = True
while login_refresh:
try:
if EC.url_matches('https://info.bbdc.sg/members-login/?err=Please refresh your browser and fill in the fields'
' properly to login. ' or 'https://info.bbdc.sg/members-login/'):
browser.refresh()
login_id_elem = browser.find_element(By.ID, 'txtNRIC')
login_pw_elem = browser.find_element(By.ID, 'txtPassword')
access_btn = browser.find_element(By.ID, 'loginbtn')
login_id_elem.send_keys("xxxx")
login_pw_elem.send_keys("xxxx")
time.sleep(5)
access_btn.click()
except NoSuchElementException:
login_refresh = False
WebDriverWait(browser, 10).until(EC.url_matches('http://www.bbdc.sg/bbdc/bbdc_web/header2.asp'))
send_anyway = browser.find_element(By.ID, 'proceed-button')
send_anyway.click()
However, I get this stacktrace error while running headless chrome. If i were to use the normal headful chrome, it works perfectly fine.
> C:\Users\Jonathan\PycharmProjects\BBDCBOT\BBDC_trial_headless.py:11:
> DeprecationWarning: executable_path has been deprecated, please pass
> in a Service o
> bject
> browser = webdriver.Chrome(executable_path=r'C:\Users\Jonathan\Projects\drivers\chromedriver_win32_V96\chromedriver.exe',options=browser_options)
>
> DevTools listening on ws://127.0.0.1:59570/devtools/browser/4d8b65e5-ebd1-418e-a010-ea8e59664472
> [1212/223007.734:INFO:CONSOLE(23)] "JQMIGRATE: Migrate is installed with logging active, version 1.4.1", source:
> https://info.bbdc.sg/wp-content/plugins
> /enable-jquery-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp
> (23)
> [1212/223007.800:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.fn.load() is deprecated", source:
> https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-he
> lper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
> [1212/223007.800:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jquery-migrate/jquer
> y-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
> [1212/223007.999:INFO:CONSOLE(825)] "Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')", source:
> https://info.bbdc.sg/memb
> ers-login/ (825)
> [1212/223008.021:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.browser is deprecated", source:
> https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-help
> er/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
> [1212/223008.022:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jquery-migrate/jquer
> y-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
> [1212/223008.057:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.attrFn is deprecated", source:
> https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helpe
> r/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
> [1212/223008.058:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jquery-migrate/jquer
> y-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
> [1212/223008.077:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.fn.unload() is deprecated", source:
> https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-
> helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
> [1212/223008.078:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jquery-migrate/jquer
> y-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
> [1212/223008.990:INFO:CONSOLE(23)] "JQMIGRATE: Migrate is installed with logging active, version 1.4.1", source:
> https://info.bbdc.sg/wp-content/plugins
> /enable-jquery-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp
> (23)
> [1212/223009.021:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.fn.load() is deprecated", source:
> https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-he
> lper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
> [1212/223009.022:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jquery-migrate/jquer
> y-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
> [1212/223009.039:INFO:CONSOLE(825)] "Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')", source:
> https://info.bbdc.sg/memb
> ers-login/ (825)
> [1212/223009.050:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.browser is deprecated", source:
> https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-help
> er/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
> [1212/223009.050:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jquery-migrate/jquer
> y-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
> [1212/223009.077:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.attrFn is deprecated", source:
> https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helpe
> r/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
> [1212/223009.078:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jquery-migrate/jquer
> y-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
> [1212/223009.105:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.fn.unload() is deprecated", source:
> https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-
> helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
> [1212/223009.105:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jquery-migrate/jquer
> y-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
> [1212/223018.583:INFO:CONSOLE(23)] "JQMIGRATE: Migrate is installed with logging active, version 1.4.1", source:
> https://info.bbdc.sg/wp-content/plugins
> /enable-jquery-migrate-helper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp
> (23)
> [1212/223018.602:INFO:CONSOLE(56)] "JQMIGRATE: jQuery.fn.load() is deprecated", source:
> https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-he
> lper/js/jquery-migrate/jquery-migrate-1.4.1-wp.js?ver=1.4.1-wp (56)
> [1212/223018.602:INFO:CONSOLE(58)] "console.trace", source: https://info.bbdc.sg/wp-content/plugins/enable-jquery-migrate-helper/js/jquery-migrate/jquer
> y-migrate-1.4.1-wp.js?ver=1.4.1-wp (58)
> [1212/223018.605:INFO:CONSOLE(385)] "Mixed Content: The page at 'https://info.bbdc.sg/members-login-redirect/' was loaded over a
> secure connection, but
> contains a form that targets an insecure endpoint 'http://www.bbdc.sg/bbdc/bbdc_web/header2.asp'. This endpoint should
> be made available over a secure c
> onnection.", source: https://info.bbdc.sg/members-login-redirect/ (385)
> [1212/223018.620:INFO:CONSOLE(388)] "Mixed Content: The page at 'https://info.bbdc.sg/members-login-redirect/' was loaded over a
> secure connection, but
> contains a form that targets an insecure endpoint 'http://www.bbdc.sg/bbdc/bbdc_web/header2.asp'. This endpoint should
> be made available over a secure c
> onnection.", source: https://info.bbdc.sg/members-login-redirect/ (388)
> Traceback (most recent call last):
> File "C:\Users\Jonathan\PycharmProjects\BBDCBOT\BBDC_trial_headless.py",
> line 30, in <module>
> WebDriverWait(browser, 10).until(EC.url_matches('http://www.bbdc.sg/bbdc/bbdc_web/header2.asp'))
> File "C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\support\wait.py",
> line 89, in until
> raise TimeoutException(message, screen, stacktrace)
> selenium.common.exceptions.TimeoutException: Message:
CodePudding user response:
For selenium 4 desired capabilities replaced with browser options.
from selenium.webdriver.chrome.options import Options
browser_options = Options()
browser_options.headless = True
driver = webdriver.Chrome(executable_path='G:\chromedriver\chromedriver.exe',options=browser_options)
CodePudding user response:
I know two ways, and they works fine.
Using
add_argument headless
options = webdriver.ChromeOptions() options.add_argument("--headless") driver = webdriver.Chrome(options = options)
Using
.headless = True
options = webdriver.ChromeOptions() options.headless = True driver = webdriver.Chrome(options = options)