Chrome Selenium But Not Headless Chrome

I developed a couple of Python scripts using Selenium and, firstly, PhantomJS. Heading for automatic downloads, I switched to (headed) Firefox (which worked), and then to Chrome with the headless option to prevent the browser from opening in front of me.

My first script that accesses a page and several HTML elements works fine with Chrome without a head.

The second, however, only works with Chrome . If I add a headless option, it no longer works. When I try to print the HTML code offline to understand why it cannot find the HTML element I need, all I have is:

<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html> 

With the title of Chrome, I have printed the full HTML code. Here's how I run my headless Chrome:

 options = webdriver.ChromeOptions() options.add_argument("--ignore-certificate-errors") options.add_argument("headless") driver = webdriver.Chrome(chrome_options=options) 

Again, note that this works in another scenario of mine. The only difference here is that I need to log in to access the page, but even then, why will it work with my head? My script is designed to automatically log in anyway by filling out a form.

Python: 3.6.1, Chrome: 60.0.3112.78 (64 bits), Selenium: 3.4.3

Any idea? Thanks.

** UPDATE: Here is the beginning of the code **

 url = 'https://10.11.227.21/tmui/' driver.get(url + "login.jsp") html_source = driver.page_source print(html_source) blocStatus = WebDriverWait(driver, TIMEOUT).until(EC.presence_of_element_located((By.ID, "username"))) inputElement = driver.find_element_by_id("username") inputElement.send_keys('actualLogin') inputElement = driver.find_element_by_id("passwd") inputElement.send_keys('actualPassword') inputElement.submit() 
+13
source share
3 answers

I had the same experience as you, and I solved it with xvfb and pyvirtualdisplay.

I use chromedrive = v2.3.1, chrome-browser = v60 and Selenium = 3.4.3

In Headless Chrome, some scripts do not work as expected.

Please refer to vpassapera comment at https://gist.github.com/addyosmani/5336747 .

How about trying this as shown below

 from pyvirtualdisplay import Display display = Display(visible=0, size=(800, 600)) display.start() # Do Not use headless chrome option # options.add_argument('headless') url = 'https://10.11.227.21/tmui/' driver.get(url + "login.jsp") html_source = driver.page_source print(html_source) blocStatus = WebDriverWait(driver, TIMEOUT).until(EC.presence_of_element_located((By.ID, "username"))) inputElement = driver.find_element_by_id("username") inputElement.send_keys('actualLogin') inputElement = driver.find_element_by_id("passwd") inputElement.send_keys('actualPassword') inputElement.submit() display.stop() 

xvfb required to use "pyvortualdisplay"

 $ sudo apt-get install -y xvfb 
+5
source

Headless Chrome does not support insecure certificates and, therefore, websites with insecure certificates do not open, remaining empty. You need to add the following features:

 from selenium import webdriver from selenium.webdriver import DesiredCapabilities from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument("--headless") capabilities = DesiredCapabilities.CHROME.copy() capabilities['acceptSslCerts'] = True capabilities['acceptInsecureCerts'] = True driver = webdriver.Chrome(chrome_options = chrome_options,executable_path='your path',desired_capabilities=capabilities) driver.get("yourWebsite") 

This will do the job.

+5
source

Headless chrome can be faster on a single machine than at the head, try adding some waiting before looking for a password item.

+1
source

Source: https://habr.com/ru/post/1270316/


All Articles