How To Automate Job Search And Email The Result Using Selenium

Hello everybody, today we will show you a very interesting but not too long selenium automation example. In this example, we will use selenium Webdriver to search jobs from monster.com automatically and then parse out the job list on the result page. After that send the job list to your favorite email box. All the process is automated. Below are the steps in the selenium Webdriver script for this action.

1. Using Selenium To Automate Sending Search Job List Example Steps.

  1. Open the Firefox browser.
  2. Browse the website monster.com
  3. Find the query keyword web element.
  4. Enter “java test” in the query keyword input text box. From the Firefox inspector, we can find the query keyword’s id is q.
  5. Find the submit button web element.
  6. Click the submit button to perform the search. From the Firefox inspector, we can find the query submit button’s id is doQuickSearch2.
  7. Parse out the job list on the search result page. From the Firefox inspector panel, we can find the following XPath.
     Result list's XPath : "//section[@id='resultsWrapper']/div[@class='js_result_container clearfix primary']".

    The XPath above can parse out all the rows in the result list. And each row includes Job title, company, and location information.

    Job title XPath : ".//div[@class='jobTitle']"
    Job company XPath : ".//div[@class='company']"
    Job locaion XPath : ".//div[@class='row']"

    Please note the dot at the beginning of each XPath above, which means to find the current web element’s child nodes.

  8. Send the job list to your favorite email box.
  9. Below is the java code using selenium Webdriver API, you can see detailed explanations in the comments.
  10. Before running the following java code, you need to add mail.jar and activation.jar to your java project.
  11. You also need to install Apache James ( an SMTP email server ) on your local machine.
    @Test
    /* This is the main test method which implement the test steps use selenium webdriver. */
    public void findTestingJobs() throws InterruptedException
    {
    /* First create Firefox driver object use selenium webdriver FirefoxDriver. */
    WebDriver ffDriver = new FirefoxDriver();
    
    /* Maximize the Firefox browser window. */
    ffDriver.manage().window().maximize();
    
    /* Open monster.com homepage which is a job finding website.*/
    ffDriver.get("https://www.monster.com/");
    
    /* Sleep 10 seconds to wait for the page load complete.*/
    Thread.sleep(10000);
    
    /* Find the search keyword web element by it's id. */
    By byIdQuery = By.id("q2");
    WebElement queryElement = this.ifWebElementExist(ffDriver, byIdQuery);
    if(queryElement!=null)
    {
    /* Input search keyword into the keyword test box. */
    queryElement.sendKeys("java test");
    }
    
    /* Find the search submit button web element by it's id. */
    By byIdSearchBtn = By.id("doQuickSearch2");
    WebElement searchBtnElement = this.ifWebElementExist(ffDriver, byIdSearchBtn);
    if(searchBtnElement!=null)
    {
    /* Click the submit button to perform the job search. */
    searchBtnElement.click();
    
    /* Wait 10 seconds to let the search result page load complete. */
    Thread.sleep(10000);
    }
    
    /* Parse out the job result list by following XPath. */
    By byXpathResultList = By.xpath("//section[@id='resultsWrapper']/div[@class='js_result_container clearfix primary']");
    
    /* Get the job result list object. */
    List<WebElement> resultList = ffDriver.findElements(byXpathResultList);
    
    if(resultList!=null)
    {
    /* This object is used to store job list info parsed out. */
    StringBuffer jobListBuf = new StringBuffer();
    
    /* Get the size of the job result list. */
    int size = resultList.size();
    
    /* Loop in the job result list. */
    for(int i=0;i<size;i++)
    {
    WebElement row = resultList.get(i);
    
    /* Get the job title, company and location for each row data for one job.
    * Please notice the XPath start with dot which means find current
    * web element's child nodes.
    * */
    WebElement jobTitleElement = row.findElement(By.xpath(".//div[@class='jobTitle']"));
    String jobTitle = jobTitleElement.getText();
    
    WebElement companyElement = row.findElement(By.xpath(".//div[@class='company']"));
    String jobCompany = companyElement.getText();
    
    WebElement locationElement = row.findElement(By.xpath(".//div[@class='row']"));
    String jobLocation = locationElement.getText();
    
    jobListBuf.append("Job Title : ");
    jobListBuf.append(jobTitle);
    jobListBuf.append(" , Company : ");
    jobListBuf.append(jobCompany);
    jobListBuf.append(" , Location : ");
    jobListBuf.append(jobLocation);
    jobListBuf.append("\r\n");
    }
    
    /* Print out each job's title, company and location. */
    //System.out.println("Job Title : " + jobTitle + " , Company : " + jobCompany + " , Location : " + jobLocation);
    System.out.println(jobListBuf.toString());
    
    this.sendEmail("[email protected]", "[email protected]", "Your Searched Job List", jobListBuf.toString());
    }
    
    /* Quit webdriver and close Firefox browser*/
    ffDriver.quit();
    
    }
    /* Function: This method is used to check whether the web element exist or not.
     * Input: driver: This is the webdriver object. 
     * byCondtion: The condition used to find the web element.
     * Return: Null if the web element not exist. A web element object if it exist. 
     * */
     private WebElement ifWebElementExist(WebDriver driver, By byCondition)
     {
     WebElement ret = null;
     /* If all the input is not null. */
     if(driver!=null && byCondition!=null)
     {
     try
     {
     /* Find the web element by the condition. */
     ret = driver.findElement(byCondition);
     }catch(Exception ex)
     {
     ex.printStackTrace();
     ret = null;
     }
     }
     return ret;
     }
    
    /* Function: This method is used to send an email using SMTP server. 
     * It only worked in localhost. So you need to install a 
     * open source SMTP server such as Apache James in your local machine.
     * 
     * Need Jars: mail.jar and activation.jar
     * 
     * Input Parameters: fromEmail: The source email address.
     * toEmail: The destination email address.
     * subject: The subject of the email.
     * sendText: The text of the email.
     * 
     * */
     private void sendEmail(String fromEmail, String toEmail, String subject, String sendText)
     {
     try{
    
    /* This is the local SMTP server's host or ip.*/
     String host = "localhost"; 
     
     /* Get the Properties object to store the smtp host info. */ 
     Properties emailProperties = System.getProperties(); 
     emailProperties.setProperty("mail.smtp.host", host); 
     
     /* Create java mail seesion.*/
     Session emailSession = Session.getDefaultInstance(emailProperties); 
     
     /* Compose the email message object use session object. */ 
     MimeMessage emailMessage = new MimeMessage(emailSession); 
     
     /* Set from email address. */
     emailMessage.setFrom(new InternetAddress(fromEmail)); 
     
     /* Set to email address. */
     emailMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(toEmail));
     
     /* Set email subject. */
     emailMessage.setSubject(subject); 
     
     /* Set email content text. */
     emailMessage.setText(sendText); 
     
     /* Send the email message */ 
     Transport.send(emailMessage); 
     
     System.out.println("Email sent complete...."); 
     }catch (MessagingException ex) 
     {
     ex.printStackTrace();
     } 
     }

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.