@@ -31,12 +31,17 @@ public class Main {
3131 private static Logger logger ;
3232 static Scanner scanner ;
3333
34+ static Random rand ;
35+
3436 public static void randomSleep (int sleep ) {
3537 // print a log message
36- logger .info ("Sleeping for " + sleep + " seconds" );
37- Random rand = new Random ();
38+
3839 try {
39- Thread .sleep ((rand .nextInt (3 ) + sleep ) * 1000 );
40+ // sleep for a random number of seconds
41+ int sleeptime = rand .nextInt (3000 ) + (sleep * 1000 );
42+ Thread .sleep (sleeptime );
43+ logger .info ("Sleeping for " + sleeptime + " seconds" );
44+
4045 } catch (InterruptedException e ) {
4146 e .printStackTrace ();
4247 }
@@ -50,10 +55,11 @@ public static void randomSleep(int sleep) {
5055 MUST_HAVE = new ArrayList <String >(
5156 Arrays .asList ("senior" , "java" , "full" , "stack" , "microservices" , "engineer" , "software" , "backend" , "spring" , "boot" , "frontend" ));
5257 scanner = new Scanner (System .in );
58+ rand = new Random ();
5359 Properties properties = new Properties ();
5460 FileInputStream in = null ;
5561 try {
56- in = new FileInputStream ("../../.. /credentials.properties" );
62+ in = new FileInputStream ("/Users/pheonix/IdeaProjects/Automation /credentials.properties" );
5763 properties .load (in );
5864 in .close ();
5965 } catch (FileNotFoundException e ) {
@@ -179,7 +185,7 @@ public static void main(String[] args) {
179185 String jobTitleInCard = aTag .getAttribute ("aria-label" );
180186
181187 // Print the content
182- System . out . println ( "aria-label content: " + jobTitleInCard );
188+
183189 // String jobTitleInCard = view.findElement(By.xpath("/html/body/div[5]/div[3]/div[4]/div/div/main/div/div[1]/div/ul/li["+i + "]/div/div[1]/div[1]/div[2]/div[1]/a")).getAttribute("aria-label");
184190 i ++;
185191
@@ -207,12 +213,16 @@ public static void main(String[] args) {
207213 continue ;
208214 }
209215
210- // chooseToApply(scanner);
216+ chooseToApply (scanner );
211217 }
212218 choice = offerUserChoices ();
213219 } while (choice );
214220 // close the browser
215- scanner .close ();
221+ closeBrowser ();
222+ }
223+
224+ private static void closeBrowser () {
225+ scanner .close ();
216226 driver .quit ();
217227 }
218228
@@ -221,7 +231,6 @@ private static boolean hasWords(String[] jobTitleInCardWords, List<String> WORDS
221231 for (String word : jobTitleInCardWords ) {
222232 if (WORDS .contains (word )) {
223233 mustHave = true ;
224- logger .info ("Found word: " + word );
225234 break ;
226235 }
227236 }
@@ -230,7 +239,7 @@ private static boolean hasWords(String[] jobTitleInCardWords, List<String> WORDS
230239
231240 private static Boolean offerUserChoices () {
232241 String jobLocation = "United States" ;
233- String jobTitle = "full stack java developer" ;
242+ String jobTitle = "java developer" ;
234243 System .out .println ("Choose from Options \n 1. Search new job Title \n 2.Change Job Location 3. exit\n " );
235244 // take input integer from user
236245 int choice = scanner .nextInt ();
@@ -261,9 +270,14 @@ private static boolean elementExists(String xpath) {
261270 }
262271
263272 private static void chooseToApply (Scanner scanner ) {
264- System .out .println ("Do you want to apply for the job? (yes/no) " );
273+ System .out .println ("Do you want to apply for the job? (y/n) e to exit " );
265274 String input = scanner .nextLine ();
266- if (input .equalsIgnoreCase ("yes" )) {
275+ if (input .equalsIgnoreCase ("e" ))
276+ {
277+ System .out .println ("Exiting the program" );
278+ closeBrowser ();
279+ }
280+ if (input .equalsIgnoreCase ("yes" ) || input .equalsIgnoreCase ("y" )) {
267281 // Click on apply button
268282 applyForThisJob ();
269283 }
@@ -277,42 +291,51 @@ private static void applyForThisJob() {
277291
278292 // xpath for next button:
279293 // /html/body/div[3]/div/div/div[2]/div/div[2]/form/footer/div[2]/button
280- List <WebElement > nextButton = driver .findElements (By .xpath (
281- "/html/body/div[3]/div/div/div[2]/div/div[2]/form/footer/div[2]/button" ));
282- int buttonAppender = 2 ;
283- while (nextButton .size () != 0 ) {
284- randomSleep (LONG_SLEEP );
285- nextButton .get (0 ).click ();
286- randomSleep (MEDIUM_SLEEP );
287- nextButton = driver .findElements (By .xpath (
288- "/html/body/div[3]/div/div/div[2]/div/div[2]/form/footer/div[2]/button[" + buttonAppender + "]" ));
289- }
294+ try {
295+ WebElement formFooter = driver .findElement (By .xpath (
296+ "/html/body/div[3]/div/div/div[2]/div/div[2]/form/footer" ));
297+ WebElement nextButton = formFooter .findElement (By .cssSelector (".artdeco-button--primary.ember-view" ));
298+ //find the button in the formFooter that has the CSS class artdeco-button--primary ember-view
299+ while (formFooter != null && nextButton != null ) {
300+ js .executeScript ("arguments[0].scrollIntoView();" , nextButton );
301+ //Log found next button
302+ logger .info ("Found next button" );
303+ //get next button aria lable
304+ String nextButtonAriaLabel = nextButton .getAttribute ("aria-label" );
305+ // log next button aria label
306+ logger .info ("Next button aria label: " + nextButtonAriaLabel );
307+ nextButton .click ();
308+ //Log Clicked next button
309+ logger .info ("Clicked next button" );
310+ randomSleep (SHORT_SLEEP );
311+ nextButton = formFooter .findElement (By .cssSelector (".artdeco-button--primary.ember-view" ));
312+ }
290313
291314 // submit button xpath:
292315 // /html/body/div[3]/div/div/div[2]/div/div[2]/div/footer/div[3]/button[2]
293316 /// html/body/div[3]/div/div/div[2]/div/div[2]/div/footer/div[2]/button[2]
294- WebElement submitButton = null ;
295- try {
296- submitButton = driver .findElement (By .xpath (
297- "/html/body/div[3]/div/div/div[2]/div/div[2]/div/footer/div[3]/button[2]" ));
317+
298318 } catch (Exception e ) {
299- // Send alert to user to manually submit the application
300- logger .info ("Please submit the application manually" );
301- // Send user press to continue
302- scanner .nextLine ();
303- return ;
319+ //log exception message
320+ //print stack trace to output
321+ e .printStackTrace ();
322+ logger .info (e .getMessage ());
323+ // logger.info("Please submit the application manually");
324+ // // Send user press to continue
325+ // scanner.nextLine();
326+ // return;
304327 /// html/body/div[3]/div/div/div[2]/div/div/form/footer/div[3]/button
305328 }
306329 // scroll to the submit button
330+ WebElement submitButton = driver .findElement (By .xpath (
331+ "/html/body/div[3]/div/div/div[2]/div/div[2]/div/footer" )).findElement (By .cssSelector (".artdeco-button--primary.ember-view" ));
307332 scrollToWebElement (submitButton );
308333 submitButton .click ();
309334 randomSleep (LONG_SLEEP );
310335
311336 WebElement closeButton = driver .findElement (By .xpath ("/html/body/div[3]/div/div/button" ));
312337 closeButton .click ();
313- // Sleep for 5 seconds
314- randomSleep (SHORT_SLEEP );
315- // Wait for 5 seconds
338+ // Sleep for 5 secondsn
316339
317340 }
318341
0 commit comments