C#でSeleniumを用いて、Facebookのページ内にある「いいね!」を自動的にクリックするサンプル。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using OpenQA.Selenium;
namespace LikeClicker
{
class Program
{
static void Main(string[] args)
{
string userID = "test@example.co.jp";
string password = "password";
// AppSettings.BrowserName.Firefoxを変更することによって対象のブラウザを変更できます
using (IWebDriver webDriver = WebDriverFactory.CreateInstance(AppSettings.BrowserName.HeadlessChrome))
{
webDriver.Url = @"https://www.facebook.com/";
IWebElement userIdElement = webDriver.FindElement(By.Id("email"));
IWebElement passwordElement = webDriver.FindElement(By.Id("pass"));
userIdElement.SendKeys(userID);
passwordElement.SendKeys(password);
userIdElement.Submit();
Console.WriteLine("認証が終了したら任意のキーを押して下さい。");
Console.Read();
string[] followURL = {
"https://www.facebook.com/pg/username1/posts/?ref=page_internal",
"https://www.facebook.com/pg/username2/posts/?ref=page_internal"};
while (true)
{
foreach (var tergetURL in followURL)
{
webDriver.Url = tergetURL;
Thread.Sleep(5000);
int findCount = 0;
foreach (var divElement in webDriver.FindElements(By.ClassName("_37uu")))
{
foreach (var refElement in divElement.FindElements(By.ClassName("UFILikeLink")))
{
if (refElement.GetAttribute("class").IndexOf("UFILinkBright") <= 0)
{
refElement.Click();
Thread.Sleep(3000);
}
}
}
}
Thread.Sleep(1000 * 60 * 10);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Safari;
namespace LikeClicker
{
internal class WebDriverFactory
{
public static IWebDriver CreateInstance(AppSettings.BrowserName browserName)
{
switch (browserName)
{
case AppSettings.BrowserName.None:
throw new ArgumentException(string.Format("Not Definition. BrowserName:{0}", browserName));
case AppSettings.BrowserName.Chrome:
return new ChromeDriver();
case AppSettings.BrowserName.HeadlessChrome:
ChromeOptions option = new ChromeOptions();
option.AddArgument("--headless");
return new ChromeDriver(option);
case AppSettings.BrowserName.Firefox:
FirefoxDriverService driverService = FirefoxDriverService.CreateDefaultService();
driverService.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
driverService.HideCommandPromptWindow = true;
driverService.SuppressInitialDiagnosticInformation = true;
return new FirefoxDriver(driverService);
case AppSettings.BrowserName.InternetExplorer:
return new InternetExplorerDriver();
case AppSettings.BrowserName.Edge:
return new EdgeDriver();
case AppSettings.BrowserName.Safari:
return new SafariDriver();
default:
throw new ArgumentException(string.Format("Not Definition. BrowserName:{0}", browserName));
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LikeClicker
{
internal static class AppSettings
{
public enum BrowserName
{
None,
Chrome,
HeadlessChrome,
Firefox,
InternetExplorer,
Edge,
Safari
}
}
}