-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
223 lines (197 loc) · 10.7 KB
/
Program.cs
File metadata and controls
223 lines (197 loc) · 10.7 KB
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
220
221
222
223
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
using System.IO;
using System.Linq;
class Program
{
static void Main(string[] args)
{
// Set up the Chrome driver
IWebDriver driver = new ChromeDriver();
try
{
// Navigate to LinkedIn login page
driver.Navigate().GoToUrl("https://www.linkedin.com/login");
// Find the username and password fields and enter your credentials
// Retrieve credentials from environment variables
string username = "ranjith1901@gmail.com";
string password = "Robo1901@";
// Find the username and password fields and enter your credentials
driver.FindElement(By.Id("username")).SendKeys(username);
driver.FindElement(By.Id("password")).SendKeys(password);
// Click the login button
driver.FindElement(By.XPath("//button[@type='submit']")).Click();
// Wait for the page to load
Thread.Sleep(5000);
// Navigate to the jobs section
driver.Navigate().GoToUrl("https://www.linkedin.com/jobs/");
// Wait for the jobs page to load
Thread.Sleep(5000);
IWebElement titleField = driver.FindElement(By.CssSelector("input[placeholder='Title, skill or company']"));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", titleField);
titleField.SendKeys("Senior .NET Developer");
IWebElement locationField = driver.FindElement(By.CssSelector("input[placeholder='Search jobs / European Union']"));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", locationField);
locationField.SendKeys("Europe");
driver.FindElement(By.CssSelector("button.jobs-search-box__submit-button")).Click();
// Enter job title and location
//driver.FindElement(By.CssSelector("input[placeholder='Title, skill or company']")).SendKeys("Senior .NET Developer");
//driver.FindElement(By.CssSelector("input[placeholder='Search jobs / European Union']")).SendKeys("Europe");
//driver.FindElement(By.CssSelector("button.jobs-search-box__submit-button")).Click();
Thread.Sleep(5000);
// Example: Click on the first job listing
driver.FindElement(By.CssSelector(".job-card-container__link")).Click();
Thread.Sleep(5000);
// Get all job listings
// Find all job cards
IList<IWebElement> jobCards = driver.FindElements(By.CssSelector(".job-card-container"));
foreach (var jobListing in jobCards)
{
try
{
if (jobListing.Text.Contains("Easy Apply"))
{
// Click on the job listing
jobListing.Click();
// Wait for the job details page to load
Thread.Sleep(5000);
// Check if the "Easy Apply" button is available
var easyApplyButton = driver.FindElements(By.CssSelector(".jobs-apply-button")).FirstOrDefault();
if (easyApplyButton != null)
{
easyApplyButton.Click();
// Add more steps to fill out the application form
// Method to click a button using JavaScript
void ClickButtonUsingJavaScript(string buttonText)
{
IWebElement button = null;
try
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
button = driver.FindElement(By.XPath($"//button[.//span[text()='{buttonText}']]"));
if (button != null && button.Displayed)
{
button.Click();
}
if (button.Displayed)
{
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", button);
}
}
catch (WebDriverTimeoutException)
{
Console.WriteLine($"{buttonText} button was not found.");
LogError($"{buttonText} button was not found.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while clicking {buttonText} button: " + ex.Message);
LogError($"An error occurred while clicking {buttonText} button: " + ex.Message);
}
}
// Method to repeatedly click the "Next" button until it is no longer available
void ClickNextButtonMultipleTimes()
{
while (true)
{
try
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
IWebElement nextButton = null;
try
{
nextButton = driver.FindElement(By.XPath("//button[.//span[text()='Next']]"));
if (nextButton != null && nextButton.Displayed)
{
nextButton.Click();
}
}
catch (WebDriverTimeoutException)
{
// Handle the timeout exception if the element is not found within the specified time
Console.WriteLine("No more 'Next' buttons found. Moving to 'Review' or 'Submit'.");
break;
}
catch (NoSuchElementException)
{
// Handle the case where the element is not found
Console.WriteLine("No 'Next' button found.");
break;
}
if (nextButton != null && nextButton.Displayed)
{
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", nextButton);
Thread.Sleep(2000); // Wait for the next section to load
}
else
{
Console.WriteLine("No more 'Next' buttons found. Moving to 'Review' or 'Submit'.");
// Attempt to click the "Review" button
ClickButtonUsingJavaScript("Review");
// Attempt to click the "Submit" button
ClickButtonUsingJavaScript("Submit");
break;
}
}
catch (WebDriverTimeoutException)
{
Console.WriteLine("No more 'Next' buttons found. Moving to 'Review' or 'Submit'.");
// Attempt to click the "Review" button
ClickButtonUsingJavaScript("Review");
// Attempt to click the "Submit" button
ClickButtonUsingJavaScript("Submit");
break;
}
catch (Exception ex)
{
Console.WriteLine("An error occurred while clicking 'Next' button: " + ex.Message);
LogError("An error occurred while clicking 'Next' button: " + ex.Message);
break;
}
}
}
// Check and click "Next" button multiple times
ClickNextButtonMultipleTimes();
// Check and click "Review" button using JavaScript
ClickButtonUsingJavaScript("Review");
// Finally, submit the form using JavaScript
ClickButtonUsingJavaScript("Submit");
}
// Go back to the job listings page
driver.Navigate().Back();
Thread.Sleep(5000);
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
LogError("An error occurred: " + ex.Message);
}
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
LogError("An error occurred: " + ex.Message);
}
finally
{
// Close the browser
driver.Quit();
}
// Keep the command line open
Console.WriteLine("Press Enter to close...");
Console.ReadLine();
}
static void LogError(string message)
{
string logFilePath = "error_log.txt";
using (StreamWriter writer = new StreamWriter(logFilePath, true))
{
writer.WriteLine($"{DateTime.Now}: {message}");
}
}
}