forked from nylonmalina/linkedinbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Browser.cs
219 lines (194 loc) · 7.55 KB
/
Browser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
using System.Linq;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.Events;
using OpenQA.Selenium.Support.Extensions;
using NLog;
using System.IO;
using OpenPop.Pop3;
using System.Drawing.Imaging;
namespace BotLinkedIn
{
public class Browser
{
private static Browser instance;
private Logger log = LogManager.GetCurrentClassLogger();
public IWebDriver driver;
//private IWebDriver driver;
private EventFiringWebDriver firingDriver;
private FirefoxProfile profile;
private string handleLinkedIn;
private string handleCrm;
public static Browser Instance
{
get
{
if (instance == null)
{
instance = new Browser();
}
return instance;
}
}
public void Start()
{
InitProfile();
//InitExtensions();
InitDriver();
// Настроить Proxy server
IniProxy("proxy.smart.argus", 3128, "", "");
InitFiringDriver();
driver.Manage().Window.Maximize();
log.Info("Start Firefox.");
handleLinkedIn = driver.WindowHandles[0];
OpenNewTab();
return ;
}
private void OpenNewTab()
{
IJavaScriptExecutor js = (IJavaScriptExecutor)driver; ;
// Открыть новое окно
js.ExecuteScript("window.open(\"\")", "");
handleCrm = driver.WindowHandles[1];
return;
}
public void Quit()
{
if (driver == null)
{
return;
}
// Закрыть окна
driver.SwitchTo().Window(handleLinkedIn).Close();
driver.SwitchTo().Window(handleCrm).Close();
// Закрыть browser
driver.Quit();
driver = null;
log.Info("Close Firefox.");
}
public void SetCurrentWindow(int cur)
{
if (cur == 0)
{
driver.SwitchTo().Window(handleLinkedIn);
}
else
{
driver.SwitchTo().Window(handleCrm);
}
}
public void LinkedInNavigateTo(string Url)
{
driver.SwitchTo().Window(handleLinkedIn);
driver.Navigate().GoToUrl(Url);
return;
}
public void CrmNavigateTo(string Url)
{
driver.SwitchTo().Window(handleCrm);
driver.Navigate().GoToUrl(Url);
return;
}
public IWebElement LinkedFindElement(By by)
{
//driver.SwitchTo().Window(handleLinkedIn);
return driver.FindElement(by);
}
public IWebElement CrmFindElement(By by)
{
//driver.SwitchTo().Window(handleCrm);
return driver.FindElement(by);
}
private void FiringDriverNavigating(object sender, WebDriverNavigationEventArgs e)
{
log.Info(String.Format("Navigating to: {0}", e.Url));
}
private void FiringDriverNavigated(object sender, WebDriverNavigationEventArgs e)
{
log.Info(String.Format("Navigated to: {0}", e.Url));
}
private void FiringDriverExceptionThrown(object sender, WebDriverExceptionEventArgs e)
{
string timestamp = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss");
string fileName = String.Format("Exception-{0}.png", timestamp);
fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, (@"Logs\" + fileName));
driver.TakeScreenshot().SaveAsFile(fileName, ImageFormat.Png);
log.Error(String.Format("Take screenshot: {0}", fileName));
}
public IWebDriver Driver()
{
return driver;
}
public IWebDriver FiringDriver()
{
return firingDriver;
}
private void InitProfile()
{
FirefoxProfileManager profileManager = new FirefoxProfileManager();
// Firefox profile name: default, WebDriver,WebLinked
profile = profileManager.GetProfile("WebDriver");
//profile = profileManager.GetProfile("WebLinked");
profile.AcceptUntrustedCertificates = true;
profile.EnableNativeEvents = true;
profile.SetPreference("browser.startup.homepage_override.mstone", "ignore");
log.Info("Profile inited:");
log.Info(" directory: {0}", profile.ProfileDirectory);
}
private void IniProxy(string proxy, int port, string userName, string password)
{
profile.SetPreference("network.proxy.http", proxy);
profile.SetPreference("network.proxy.http_port", port);
profile.SetPreference("network.proxy.ftp", proxy);
profile.SetPreference("network.proxy.ftp_port", port);
profile.SetPreference("network.proxy.socks", proxy);
profile.SetPreference("network.proxy.socks_port", port);
profile.SetPreference("network.proxy.ssl", proxy);
profile.SetPreference("network.proxy.ssl_port", port);
profile.SetPreference("network.proxy.user_name", userName);
profile.SetPreference("network.proxy.password", password);
profile.SetPreference("network.proxy.share_proxy_settings", true);
// Proxy type: Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
profile.SetPreference("network.proxy.type", 5);
log.Info("Change proxy server settings to:");
log.Info(" network.proxy.http: {0}", proxy);
log.Info(" network.proxy.hhttp_port: {0}", port);
profile.SetPreference("network.proxy.no_proxies_on", "localhost, 127.0.0.1,crm.smart.argus");
}
private void InitExtensions()
{
var extensions = new List<string>();
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Extensions");
extensions.Clear();
if (Directory.Exists(path))
{
var files = Directory.EnumerateFiles(path, "*.xpi");
foreach (string file in files)
{
profile.AddExtension(file);
}
}
else
{
log.Warn(String.Format("Extensions directory not found: {0}.", path));
}
}
private void InitDriver()
{
driver = new FirefoxDriver(new FirefoxBinary(), new FirefoxProfile(), TimeSpan.FromSeconds(180));
//driver = new FirefoxDriver();
// Установить максимальное время загрузки страницы
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(Settings.Selenium.PageLoadTimeout));
return;
}
private void InitFiringDriver()
{
firingDriver = new EventFiringWebDriver(driver);
firingDriver.Navigating += FiringDriverNavigating;
firingDriver.Navigated += FiringDriverNavigated;
firingDriver.ExceptionThrown += FiringDriverExceptionThrown;
}
}
}