0

I made a python script that scrapes information and clicks a few buttons and it works great on chrome webdriver, but as soon as I try to enter it as headless it does nothing for a few seconds and then gives an error. My code is below.

options = Options()
options.headless = True
options.add_argument("user-data-dir=selenium")
browser = webdriver.Chrome(executable_path=r'C:\Users\REDACTED\Desktop\REDACTED\browser\chromedriver.exe', options=options)
browser.get('https://REDACTED')

And here's the error

Traceback (most recent call last):
  File "C:/Users/REDACTED/PycharmProjects/Test/REDACTED.py", line 49, in <module>
    browser = webdriver.Chrome(executable_path=r'C:\Users\REDACTED\Desktop\REDACTED\browser\chromedriver.exe', options=options)
  File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
  (Driver info: chromedriver=2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90),platform=Windows NT 10.0.17134 x86_64)
Xetra
  • 73
  • 1
  • 8

1 Answers1

0

What works for me when I use selenium with chrome is:

you can try options.add_argument('--headless') as your method brings up an error (only for chrome, no error for firefox)

another thing you can try is removing the .exe from your executable_path. When i add .exe i get a the same error you got.

The BrownBatman
  • 2,593
  • 1
  • 13
  • 29
  • the .exe works in normal chrome mode though, what makes it not work in headless? – Xetra Dec 04 '18 at 00:50
  • In all honesty, I have no idea. I was initially curious to find the answer but to no avail - I'd be happy for someone more experienced to chime in. Did your problem fix? – The BrownBatman Dec 04 '18 at 00:53
  • No I tried both your solutions and still the same problem. The weird thing is that it takes like 1,5 minute for it to show my error, when running with normal chromedriver it takes 3-5 seconds to run the whole script and no errors – Xetra Dec 04 '18 at 00:59