2

I am trying to Automate my WhatsApp to send messages in bulk using pyautogui. I was able to send them but for every message, a new tab pops in and my work gets disturbed. How can I make the WhatsApp web tab run in the background or in another window without disturbing my work? Here is my code. Iam getting an error:

TypeError                                 Traceback (most recent call last)
<ipython-input-1-3555dd798e71> in <module>
     10 chrome_options.add_argument("--headless")
     11 
---> 12 driver = webdriver.Chrome(executable_path=r"C:/Users/AB/chromedriver", chrome_options=Options)
     13 
     14 data = pd.read_excel("C:/Users/AB/Desktop/contacts2.xlsx")

C:\anaconda\lib\site-packages\selenium\webdriver\chrome\webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive) 62         else:
     63             if desired_capabilities is None:
---> 64                 desired_capabilities = options.to_capabilities()
     65             else:
     66                 desired_capabilities.update(options.to_capabilities())

**TypeError: to_capabilities() missing 1 required positional argument: 'self'**

Here is the updated code:

import pyautogui as pg
#import webbrowser as web
import time
import pandas as pd
from selenium import webdriver 
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")

driver = webdriver.Chrome(executable_path=r"C:/Users/AB/chromedriver", chrome_options=Options)

data = pd.read_excel("C:/Users/AB/Desktop/contacts2.xlsx")
data_dict = data.to_dict('list')
leads = data_dict['contact']
messages = data_dict['msg']
combo = zip(leads,messages)
first = True
for lead,message in combo:
    time.sleep(6)
    driver.get("https://web.whatsapp.com/send?phone="+lead+"&text="+message)
    if first:
        time.sleep(8)
        first=False
    width,height = pg.size()
    pg.click(width/2,height/2)
    time.sleep(8)
    pg.press('enter')
    time.sleep(8)
    pg.hotkey('ctrl', 'w')```
Bandi
  • 21
  • 3

1 Answers1

4

To run chrome-headless just add --headless in chrome_options.add_argument, i.e.:

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

chrome_options = Options()
chrome_options.add_argument("--headless")

In that case you will not see chrom window.

Gaj Julije
  • 1,538
  • 15
  • 32
  • Thankyou verymuch, however, Iam getting an error of ''TypeError: to_capabilities() missing 1 required positional argument: 'self', after adding your code and specifying the chromedriver path. – Bandi Feb 01 '21 at 11:08
  • 1
    See https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self – Gaj Julije Feb 01 '21 at 12:06
  • can u post some other link, as iam unable to figure out out from whats been posted – Bandi Feb 12 '21 at 13:22