Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminate entering QR - Whatsapp web automated by selenium - Java

I'm trying to eliminate the process of the QR code after the first I'm doing it.

My code to run Whatsapp trough a web driver:

public class DriverTester {

public static void main(String[] args) {

    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
    ChromeDriverService service = new ChromeDriverService.Builder()
            .usingDriverExecutable(new File("C:\\whatsup\\src\\main\\resources\\chromedriver.exe"))
            .usingAnyFreePort()
            .build();

    ChromeOptions options = new ChromeOptions();
    options.merge(capabilities);
    ChromeDriver driver = new ChromeDriver(service, options);

    driver.navigate().to("https://web.whatsapp.com/");

    while (driver.findElements(By.xpath(XPaths.autoStartReady)).size() == 0);

    LocalStorage localStorage = driver.getLocalStorage();

    driver.close();

    driver = new ChromeDriver(service, options);

    for (String key : localStorage.keySet()){
        String value = localStorage.getItem(key);
        driver.executeScript("window.localStorage.setItem('"+key+"', '"+value+"');");
    }

    driver.navigate().to("https://web.whatsapp.com/");

}

}

When the web browser opens for the first time, I'm doing the QR code routine on my phone. The exception was raised while executing the javascript code.

But I'm getting the next exception:

Exception in thread "main" org.openqa.selenium.NoSuchSessionException: no such session

If I'm trying to save the cookies, the cookies set is empty (cannot save any cookie, still don't know why).

How can I skip this process of the QR after the first time? If someone knows how to skip it without even doing it once, it will be also helpful (but I think too much difficult for now).

Thanks in advance!

like image 314
Roni Koren Kurtberg Avatar asked Apr 14 '18 13:04

Roni Koren Kurtberg


People also ask

How can I remove QR code from WhatsApp Web?

If you delete your WhatsApp account, your WhatsApp QR code will also be deleted. Open WhatsApp > tap More options > Settings. Tap the QR icon displayed next to your name. Tap More > Reset QR code > RESET > OK.

Can we automate WhatsApp using selenium?

Pywhatsapp is used to Automate Whatsapp through Whatsapp web. We can add number of contacts whom we want to send messages or Media attachments ( like Video or Images). Selenium, Autoit and Schedule have been used one from Automation and other for Scheduling messages.

How do I automate WhatsApp Web?

Automate Whatsapp. copy the code above and paste it in automation.py, run it, and it will show a Whatsapp Web interface. Scan the QR code, wait until the loading is complete and press enter on the terminal/command prompt to continue the process to send the messages.


1 Answers

Marcelo's answer is correct but poorly explained in my opinion, so here's what worked for me:

Create a folder in your project directory to store all the cache that the chromedriver session will create (it's also nice to ignore the content in your .gitignore file).

enter image description here

And then, start the webdriver options and set the directory.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

options = webdriver.ChromeOptions()
options.add_argument(r"user-data-dir=./driver/data")
driver = webdriver.Chrome(executable_path=r'./driver/chromedrive84_win', options=options)
driver.get('https://web.whatsapp.com/')
like image 80
jvbs Avatar answered Oct 18 '22 18:10

jvbs