Identicon
Identicon

Reputation: 403

In Python, how do I make Selenium work headless with a Saved Browser Session?

I'm trying to bypass the web.whatsapp.com QR scan page. This is the code I used so far:

options = webdriver.ChromeOptions();
options.add_argument('--user-data-dir=./User_Data')
driver = webdriver.Chrome(options=options)
driver.get('https://web.whatsapp.com/')

On first attempt i have to manually scan the QR code and on later attempts it doesn't ask for the QR code.

HOWEVER, if i try to do the same after adding this line chrome_options.add_argument("--headless") I get Error writing DevTools active port to file. I tried at least a dozen different google search solutions, but none of them are working. Any help on this would be highly appreciated! Thank you.

Tried a bunch of differet arguments in different combinations so far but nothing worked:

options = Options() #decomment for local debugging
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-setuid-sandbox')
options.add_argument('--remote-debugging-port=9222')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')  # Last I checked this was necessary.
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument('--user-data-dir=./User_Data')
driver = webdriver.Chrome('chromedriver.exe', options=options)

driver.get('https://web.whatsapp.com/')

Upvotes: 1

Views: 3124

Answers (1)

Gabriel G Filleti
Gabriel G Filleti

Reputation: 76

Recently I made a whatsapp bot and had the same problems. After searching for a long time I came up with this solution:

The first problem was the browser cache memory, if it doesn't get the QR code cached in the browser apdata it will keep waiting in order to scan it.

So in my program I used the following function to get:

def apdata_path():
    path = str(pathlib.Path().absolute())
    driver_path = path + "\chromedriver.exe"
    apdata = os.getenv('APPDATA')
    apdata_path = "user-data-dir=" + \
    re.findall(".+.\Dta\D", a)[0] + \
    r'Local\Chromium\User Data\Default\Default'
    apdata_path = apdata_path.replace("\\", "\\"*2)
    return apdata_path

Here it finds first apdata path => C:\Users\AppData\ then I concatenated the rest of the path to the cache folder, in this case I used Chromium. In your case it will be:

C:\Users\AppData\Local\Google\Chrome\User Data\Default

There's probably a better way to find the profile data path. After finding it I set the driver:

def chrome_driver(user_agent=0):
    usr_path = apdata_path()
    chrome_path = file_path() + '\Chromium 85\\bin\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = r"{}".format(chrome_path)
    if user_agent != 0:
        options.add_argument('--headless')
        options.add_argument('--hide-scrollbars')
        options.add_argument('--disable-gpu')
        options.add_argument("--log-level=3")
        options.add_argument('--user-agent={}'.format(user_agent))
    options.add_argument(usr_path)
    driver = webdriver.Chrome('chromedriver.exe', chrome_options=options)
    return driver

Here I had another problem, that is, sometimes Selenium wouldn't work because Whatsapp has user agent validation in order to be able to verify if the browser version its compatible. I don't know much so I reached this conclusion by trial and error, maybe this is not the real explanation. But it worked for me.

So, in my Bot i made a start function to get the user agent and get the first QR Scan and keep it on the browser cache:

def whatsapp_QR():
    driver = chrome_driver()
    user_agent = driver.execute_script("return navigator.userAgent;")
    driver.get("https://web.whatsapp.com/")
    print("Scan QR Code, And then Enter")
    input()
    print("Logged In")
    driver.close()
    return user_agent

Afterall, my bot worked, not perfectly but it ran smoothly. I was able to send messages in my test group in headless mode.

To summarize, get the profile user cache in apdata to bypass the QR Code ( but you will need to run it once without headless to create the first cache).

Then get the user agent in order to bypass Wthatsapp validation. So the whole option set would look like this:

options.add_argument('--headless')
options.add_argument('--hide-scrollbars')
options.add_argument('--disable-gpu')
options.add_argument("--log-level=3")
options.add_argument('--user-agent={}'.format(user_agent)) # User agent for validation
options.add_argument(usr_path) #apdata user profile, to by pass QR code

usr_path = "user-data-dir=rC:\Users\\AppData\Local\Google\Chrome\User Data\Default"

Upvotes: 6

Related Questions