Selenium WebDriver – Search And Send Job List In Email

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 automatically and then parse out the job list in the result page. After that send the job list to your favorite email box. All the process is automated. Below is the steps in the selenium webdriver script for this action.

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

    This XPath above can parse out all the rows in the result list. And each row include 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, that means to find current web element’s child nodes.
    monster job search result list page

  8. Send the job list to your favorite email box.
  9. Below is the java code using selenium webdriver api, you can see detail explanation in the comments.

Before run following java code, you need to add mail.jar and activation.jar into your java project. The download file for this article include the two jar files.

You also need to install Apache James which is a SMTP email server in your local machine.

/* 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. */

/* Open homepage which is a job finding website.*/

/* Sleep 10 seconds to wait for the page load complete.*/

/* Find the search keyword web element by it's id. */
By byIdQuery ="q2");
WebElement queryElement = this.ifWebElementExist(ffDriver, byIdQuery);
/* 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 ="doQuickSearch2");
WebElement searchBtnElement = this.ifWebElementExist(ffDriver, byIdSearchBtn);
/* Click the submit button to perform the job search. */;

/* Wait 10 seconds to let the search result page load complete. */

/* 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);

/* 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(" , Company : ");
jobListBuf.append(" , Location : ");

/* Print out each job's title, company and location. */
//System.out.println("Job Title : " + jobTitle + " , Company : " + jobCompany + " , Location : " + jobLocation);

this.sendEmail("", "", "Your Searched Job List", jobListBuf.toString());

/* Quit webdriver and close Firefox browser*/

 /* 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)
 /* Find the web element by the condition. */
 ret = driver.findElement(byCondition);
 }catch(Exception ex)
 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)

/* 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("", 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. */
 /* Set email content text. */
 /* Send the email message */ 
 System.out.println("Email sent complete...."); 
 }catch (MessagingException ex) 

Download “” – Downloaded 22 times – 473 KB

(Visited 110 times, 1 visits today)
READ :   Selenium Webdriver Handle Firefox Or Chrome Download Popup

Leave a Reply

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