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 monster.com 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 monster.com
  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.

@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();
 } 
 }

Download “TestFindTestingJobsWithJarLibrary.zip” TestFindTestingJobsWithJarLibrary.zip – Downloaded 26 times – 473 KB

(Visited 142 times, 1 visits today)
READ :   POM(Page Object Model) Framework And Page Factory In WebDriver

Leave a Reply

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