1

I wrote a code to send a WhatsApp message using selenium and WhatsApp web, and it works great but I can't figure pot why it won't work in headless mode, it doesn't lunch the browser at all.

when I run it in headless mode and without the crome path option it succeeds to launch the browser but of course, it doesn't work because I have to have the data. this is the error message: "selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist".

I also tried to add options like '--no-sandbox' and '--disable-dev-shm-usage' but it still doesn't work.

    BASE_URL = "https://web.whatsapp.com/"
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument(config.CHROME_PROFILE_PATH)

    def main():
        driver = Chrome(executable_path='driver/chromedriver.exe',
                    options=options)
        driver.get(BASE_URL)
        driver.maximize_window()
        driver.implicitly_wait(config.WAIT_TIME)
Error Hunter
  • 1,354
  • 3
  • 13
  • 35

2 Answers2

1

Refer the below link. Launching chrome using headless is already answered there.

You can do with as little code as this, Instead of using add arguments. You can use this headless option. Hope this helps.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.headless = True
driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe)

Also see the link for various answers from different users and the accepted answer

How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/27885142) – JP. Aulet Dec 19 '20 at 11:35
  • @JP.Aulet, Thanks for letting me know. I have updated my answer. – Shashankk Sai Dec 19 '20 at 11:50
  • If this is already answered somewhere else then why did you write another answer here? – Dharman Dec 19 '20 at 11:53
  • That's my preferred answer. Doesn't mean they have to stick to this one. There are other options to explore and test before implementing it. – Shashankk Sai Dec 19 '20 at 12:08
  • it still doesnt work. when i take out the "options.add_argument(config.CHROME_PROFILE_PATH)" it works fine, but i have to have this file – test_junkie123 Dec 19 '20 at 15:57
0

You need to launch your chrome in headless mode and scan QR code.

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3312.0 Safari/537.36');
driver = webdriver.Chrome(
    executable_path=r'path\to\chromedriver.exe',
    options=options)
qr_code =  driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[1]/div/div[2]/div').screenshot_as_png
Maryam
  • 1
  • 2