简体   繁体   中英

Selenium working with Chrome, but not headless Chrome

I've developed a couple of Python scripts using Selenium and, at first, PhantomJS. While heading toward automated downloads, I switched for (headed) Firefox (which worked) and then Chrome with the headless option so I won't have the browser opening in front of me.

My first script, which accesses a page and a couple of HTML elements, works perfectly with headless Chrome.

The second one, however, works only with headed Chrome . If I add the "headless" option, it doesn't work anymore. When I try to print the HTML in headless mode to see why it cannot find the HTML element I'm looking for, all I have is :

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

With the headed Chrome, I have a complete HTML printed. This is how I start 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 of my script. The only difference here is that I need to log in to access the page, but even then, why would it work with the head? My script is made to log in automatically anyway by filling the form.

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

Any idea? Thanks.

** EDIT: 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()

I've developed a couple of Python scripts using Selenium and, at first, PhantomJS. While heading toward automated downloads, I switched for (headed) Firefox (which worked) and then Chrome with the headless option so I won't have the browser opening in front of me.

My first script, which accesses a page and a couple of HTML elements, works perfectly with headless Chrome.

The second one, however, works only with headed Chrome . If I add the "headless" option, it doesn't work anymore. When I try to print the HTML in headless mode to see why it cannot find the HTML element I'm looking for, all I have is :

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

With the headed Chrome, I have a complete HTML printed. This is how I start 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 of my script. The only difference here is that I need to log in to access the page, but even then, why would it work with the head? My script is made to log in automatically anyway by filling the form.

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

Any idea? Thanks.

** EDIT: 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()

I've developed a couple of Python scripts using Selenium and, at first, PhantomJS. While heading toward automated downloads, I switched for (headed) Firefox (which worked) and then Chrome with the headless option so I won't have the browser opening in front of me.

My first script, which accesses a page and a couple of HTML elements, works perfectly with headless Chrome.

The second one, however, works only with headed Chrome . If I add the "headless" option, it doesn't work anymore. When I try to print the HTML in headless mode to see why it cannot find the HTML element I'm looking for, all I have is :

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

With the headed Chrome, I have a complete HTML printed. This is how I start 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 of my script. The only difference here is that I need to log in to access the page, but even then, why would it work with the head? My script is made to log in automatically anyway by filling the form.

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

Any idea? Thanks.

** EDIT: 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()

I've developed a couple of Python scripts using Selenium and, at first, PhantomJS. While heading toward automated downloads, I switched for (headed) Firefox (which worked) and then Chrome with the headless option so I won't have the browser opening in front of me.

My first script, which accesses a page and a couple of HTML elements, works perfectly with headless Chrome.

The second one, however, works only with headed Chrome . If I add the "headless" option, it doesn't work anymore. When I try to print the HTML in headless mode to see why it cannot find the HTML element I'm looking for, all I have is :

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

With the headed Chrome, I have a complete HTML printed. This is how I start 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 of my script. The only difference here is that I need to log in to access the page, but even then, why would it work with the head? My script is made to log in automatically anyway by filling the form.

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

Any idea? Thanks.

** EDIT: 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()

I've developed a couple of Python scripts using Selenium and, at first, PhantomJS. While heading toward automated downloads, I switched for (headed) Firefox (which worked) and then Chrome with the headless option so I won't have the browser opening in front of me.

My first script, which accesses a page and a couple of HTML elements, works perfectly with headless Chrome.

The second one, however, works only with headed Chrome . If I add the "headless" option, it doesn't work anymore. When I try to print the HTML in headless mode to see why it cannot find the HTML element I'm looking for, all I have is :

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

With the headed Chrome, I have a complete HTML printed. This is how I start 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 of my script. The only difference here is that I need to log in to access the page, but even then, why would it work with the head? My script is made to log in automatically anyway by filling the form.

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

Any idea? Thanks.

** EDIT: 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()

I've developed a couple of Python scripts using Selenium and, at first, PhantomJS. While heading toward automated downloads, I switched for (headed) Firefox (which worked) and then Chrome with the headless option so I won't have the browser opening in front of me.

My first script, which accesses a page and a couple of HTML elements, works perfectly with headless Chrome.

The second one, however, works only with headed Chrome . If I add the "headless" option, it doesn't work anymore. When I try to print the HTML in headless mode to see why it cannot find the HTML element I'm looking for, all I have is :

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

With the headed Chrome, I have a complete HTML printed. This is how I start 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 of my script. The only difference here is that I need to log in to access the page, but even then, why would it work with the head? My script is made to log in automatically anyway by filling the form.

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

Any idea? Thanks.

** EDIT: 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()

I've developed a couple of Python scripts using Selenium and, at first, PhantomJS. While heading toward automated downloads, I switched for (headed) Firefox (which worked) and then Chrome with the headless option so I won't have the browser opening in front of me.

My first script, which accesses a page and a couple of HTML elements, works perfectly with headless Chrome.

The second one, however, works only with headed Chrome . If I add the "headless" option, it doesn't work anymore. When I try to print the HTML in headless mode to see why it cannot find the HTML element I'm looking for, all I have is :

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

With the headed Chrome, I have a complete HTML printed. This is how I start 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 of my script. The only difference here is that I need to log in to access the page, but even then, why would it work with the head? My script is made to log in automatically anyway by filling the form.

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

Any idea? Thanks.

** EDIT: 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()

For my situation, headless did not work because I was behind a proxy. Apparently, Chrome is able to use the system proxy, but headless does not use the system proxy.

I simply needed to provide the proxy, then headless (as well as Chrome) worked.

options.add_argument('--proxy-server=http://myproxy:port')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM