Search code examples
pythonselenium-webdriverselenium-chromedriver

selenium webdriver open the chrome but not the whatsappWeb


I have this code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from time import sleep

# Create a Service object by specifying the path to chromedriver
chrome_service = Service(executable_path="path\crome.exe")

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\path_file")

# Pass the Service object to webdriver.Chrome()
driver = webdriver.Chrome(service=chrome_service, options=options)

# Open WhatsApp Web
driver.get("https://web.whatsapp.com/")

sleep(10)

When I run this code chrome opens but whatsapp web doesn't open. how to fix that?

I tried using several different methods like:

from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

and:

options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")

but none of them worked


Solution

  • You don't need to specify the the chrome exectuable path anymore. It's enough that Chrome is installed and set as default browser.

    I tested this and it's working fine:

    from selenium import webdriver
    from time import sleep
    
    options = webdriver.ChromeOptions()
    options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
    #options.add_argument("user-data-dir=C:\\path_file")
    
    # Pass the Service object to webdriver.Chrome()
    driver = webdriver.Chrome(options=options)
    
    # Open WhatsApp Web
    driver.get("https://web.whatsapp.com/")
    
    sleep(10)