How to send whatsapp messages with selenium in an existing WhatsappWeb connection - c#

I tried to send messages to whatsapp with Selenium and C#.
The problem is that, if as an example I am connected to Whatsapp in the current Chrome profile, when the WebDriver is initialized it open a new chrome, without profile and with whatsapp scan QR code.
I want to send messages using the existing browser connection.
I have tried:
var chromeOptions = new ChromeOptions();
chromeOptions.AddExcludedArgument("enable-automation");
chromeOptions.AddAdditionalChromeOption("useAutomationExtension", false);
driver = new ChromeDriver(chromeOptions);
driver.Navigate().GoToUrl("https://web.whatsapp.com/");
And also:
var chromeOptions = new ChromeOptions();
chromeOptions.AddExcludedArgument("enable-automation");
chromeOptions.AddArgument("--user-data-dir=chrome-data");
chromeOptions.AddAdditionalChromeOption("useAutomationExtension", false);
driver = new ChromeDriver(chromeOptions);
driver.Navigate().GoToUrl("https://web.whatsapp.com/");
What I want: to open a new chrome instance, with the existing whatsapp connection,

You need to load the correct profile.
This is the answer provided from uzumaki on how to achieve that:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\path\to\chrome\user\data") #e.g. C:\Users\You\AppData\Local\Google\Chrome\User Data
options.add_argument(r'--profile-directory=YourProfileDir') #e.g. Profile 3
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")

Related

C# Selenium download file that "can harm my computer"

I try to download a ".cert" file which my Chrome identify to be malicious or has potential to harm my PC. Is there a possibility to disable the following box:
Popup Box.
I have searched and tried a ton of possible workarounds or solutions, but none of them worked so far. My Chromeoptions are like that:
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
chromeOptions.AddUserProfilePreference("download.prompt_for_download", false);
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");
chromeOptions.AddUserProfilePreference("download.directory_upgrade", true);
chromeOptions.AddUserProfilePreference("safebrowsing.enabled", true);
I am using the following Nuget pakages:
Selenium WebDriver Version 3.9.1
Selenium.Chrome.Webdriver Version 2.37
Best Regards,
Kandey
This works using Selenium.WebDriver 3.11.2 and Selenium.Chrome.WebDriver 2.37.0 along with disabling download protection. The blank .crt is still hosted on my blog so you can test if you like.
ChromeOptions options = new ChromeOptions();
options.AddArgument("--safebrowsing-disable-download-protection");
options.AddUserProfilePreference("safebrowsing", "enabled");
using (var driver = new ChromeDriver(ChromeDriverService.CreateDefaultService(AppDomain.CurrentDomain.BaseDirectory), options))
{
driver.Url = "https://www.kitson-online.co.uk/test.crt";
}
Chrome v.85, webdriver v.3.141
Worked for me:
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("safebrowsing", "disabled");
driver = new ChromeDriver(Environment.CurrentDirectory, options);

Click on HTML elements with Scrapy (WebScraping)

I'm doing a program in c # using scrapySharp or HtmlAgilityPack. But I have the disadvantage of that part of the information that I need, to appear when I click on an HTML element (Button, link ).
In some forums it was commented that when using Selenium you could manipulate the html elements, so I tried the following
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
// Defines the interface with the Chrome browser
IWebDriver driver = new ChromeDriver ();
// Auxiliary to store the label element in href
Element IWebElement;
// Go to the website
driver.Url = url;
// Click on the download button
driver.FindElement (By.Id ("Download button")). Click ();
but being a web automation test, it opens a browser and the website to perform the selection process (clicks), so it is not of my use, since I have to perform the inspection on several websites internally.
Although I can continue using Selenium, I am looking for ways to avoid using the browser and instead click without it.
Does anyone know how to achieve the click of the link or button, without the need to open a browser for web scraping?
Hope this would be helpful to anyone who has the same requirements.
If you want to avoid opening the browser, you could use below settings in the ChromeDriver.
// settings for avoid opening browser
var options = new ChromeOptions();
options.AddArgument("headless");
var service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
// url to access and scrape
var url = "https://example.com";
using (var driver = new ChromeDriver(service, options))
{
// access the url
driver.Navigate().GoToUrl(url);
// Click on the download button - copied from your code above
driver.FindElement (By.Id ("Download button")). Click ();
}
In addition to above below links also, you may find useful,
can-selenium-webdriver-open-browser-windows-silently-in-background
running-webdriver-without-opening-actual-browser-window

C# Mute Audio Selenium Chrome Browser

Like my title says, I am looking for a way to mute any audio on Selenium Chrome Web Driver.
I found this for python(example below) and was wondering if there was something similar for C#?
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--mute-audio")
Try this:
var options = new ChromeOptions();
options.AddArgument("mute-audio");
var driver = new ChromeDriver(options);

How to download PDF using ChromeDriver 2.32 and Chrome in version 57 or higher

Since version 57 Google did remove chrome://plugins option. I found that if I want to download PDF file instead of opening it in Chrome Viewer I need to turn on switch in chrome://settings/content/pdfDocuments
Is there any way to set this option in Chrome driver for example using preferences like below?
private ChromeOptions BuildChromeProfile()
{
var options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory", DownloadsPath);
options.AddUserProfilePreference("download.prompt_for_download", false);
return options;
}
Or any other way?
options.AddUserProfilePreference("plugins.always_open_pdf_externally", true);
Worked for me.

IE attachment using selenium

Is it possible to attach in an open internet explorer using selenium?
I've already read lots of forums about this, and they always said that it is possible for IE and not on other browsers. But the problem is, there is no available example or instruction on how to implement it.
I dont know about Internet Explorer, but you can connect to an opened firefox browser if this browser instance is previously opened by WebDriver.
IWebDriver WebDriver = null;
try
{
System.Uri uri = new System.Uri("http://localhost:7055/hub");
WebDriver = new RemoteWebDriver(uri, DesiredCapabilities.Firefox());
Console.WriteLine("Executed on remote driver");
}
catch (Exception)
{
WebDriver = new FirefoxDriver(firefoxProfile);
Console.WriteLine("Executed on New FireFox driver");
}
see this blog post for more details
http://binaryclips.com/2014/09/16/selenium-web-driver-in-c-how-to-continue-script-on-the-already-opened-browser-instance/

Categories

Resources