Solution for mod-17:Excercise no.9 | Selenium Forum
M
Posted on 27/01/2016
Following is the code sir.
This code gives expected results.
Please suggest for improvements.
==================================================================================

package p1;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class Dice_excercise {
static WebDriver w;
static WebElement L;
static String title;
static String pagenums;
static int count;
static WebElement rows_on_page;
static int length_of_string;
static String[] linknums;
static String part1;
static String part2;

public static void main(String[] args) {
// TODO Auto-generated method stub

ProfilesIni prof = new ProfilesIni();
FirefoxProfile p = prof.getProfile("Chaitra");
w=new FirefoxDriver(p);
w.get("http://www.dice.com/");
w.findElement(By.xpath("//*[@id='search-field-keyword']")).sendKeys("Java");
w.findElement(By.xpath("//*[@id='search-form']/fieldset/div[4]/div/div[1]/button")).click();
w.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);

List <WebElement>List_of_links=new ArrayList<WebElement>();
DisplayPageLinks();
for(int pagenum=2;pagenum<6;pagenum++)
{
pagenums=String.valueOf(pagenum);
System.out.println("********************************************************");
System.out.println("PageNum:"+pagenums);
System.out.println("*********************************************************");
w.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);
w.findElement(By.xpath("//a[text()="+pagenums+"]")).click();
w.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);

DisplayPageLinks();

}

}

static void DisplayPageLinks()
{
rows_on_page=w.findElement(By.xpath("//*[@id='resultSec']/div[1]/div[5]/div/h4/span[1] "));
length_of_string=rows_on_page.getText().length();
linknums=rows_on_page.getText().split("-");
part1 = linknums[0].trim();
part2 = linknums[1].trim();

count=(Integer.parseInt(part2))-(Integer.parseInt(part1));

System.out.println("count on page is"+count);



for(int i=0;i<=count;i++)
{
//L=w.findElement(By.xpath("//*[@id='serp']/div["+i+"]/h3/a"));
L=w.findElement(By.xpath("//*[@id='position"+i+"']"));
//*[@id='position0']
title=L.getAttribute("title");
if(title.contains("Java"))
{
System.out.println("All these links contain keyword <Java>"+title+"---"+i);
}
}
}}