Excercise _module 17:not able to figure out c) solution | Selenium Forum
M
Posted on 18/01/2016
2) Go to www.google.com and search for something.
a) Print names of all result links on first page.
b) Print names of all result links on first 5 pages
c) Click on each and every link on first 5 pages and check if next page is opening.
=============================================================
Sir not able to figure out c)..solution .
Also please verify my code & suggest.
Following is code for a),b)
//-------------------------------------------------------------------------------------------------------
package p1;

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

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
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 google_c {
static WebElement box2;
static List<WebElement> links2;
public static void main(String[] args) {
// TODO Auto-generated method stub





ProfilesIni prof = new ProfilesIni();
FirefoxProfile p = prof.getProfile("Chaitra");


WebDriver w=new FirefoxDriver(p);
w.get("https://www.google.com");
w.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
w.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
w.manage().window().maximize();
w.findElement(By.name("q")).sendKeys("types of kindles");
w.findElement(By.name("q")).sendKeys(Keys.ENTER);
//==========print links==1st page=========================

WebElement box=w.findElement(By.xpath("//*[@id='ires']"));
List<WebElement> links = box.findElements(By.tagName("a"));
System.out.println("Total links -> "+ links.size());
for(int i=0;i<links.size();i++)
System.out.println(links.get(i).getText());

String A="//*[@id='nav']/tbody/tr/td[";
String B="]";
int l;
//==========print links==2 to 5 pages=========================
for(int g=2;g<=5;g++)
{
System.out.println(A+g+B);
w.findElement(By.xpath("//*[contains(text(),'Next')]")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("clicked this page"+g);
box2=w.findElement(By.xpath("//*[@id='ires']"));
links2 = box2.findElements(By.tagName("a"));
l=g-1;
System.out.println("Total links_page-> page: "+g+":"+ links2.size());
for(int h=0;h<links2.size();h++)
System.out.println("Listing:"+links2.get(h).getText());
System.out.println("-----------------------------------------------");

}
}

}

M
Replied on 18/01/2016

[code:12f1isx6]package com.sample;

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

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
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 google_c {
static WebElement box2;
static List<WebElement> links2;

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

ProfilesIni prof = new ProfilesIni();
FirefoxProfile p = prof.getProfile("Chaitra");

WebDriver w = new FirefoxDriver(p);
w.get("https://www.google.com");
w.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
w.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
w.manage().window().maximize();
w.findElement(By.name("q")).sendKeys("types of kindles");
w.findElement(By.name("q")).sendKeys(Keys.ENTER);
// ==========print links==1st page=========================

WebElement box = w.findElement(By.xpath("//*[@id='res']"));
List<WebElement> links = w.findElements(By.xpath(".//*[@id='rso']/div/div/div/div/div/div/cite"));
System.out.println("Total links -> " + links.size());
for (int i = 0; i < links.size(); i++)
System.out.println(links.get(i).getText());

String A = "//*[@id='nav']/tbody/tr/td[";
String B = "]";
int l;
// ==========print links==2 to 5 pages=========================
for (int g = 2; g <= 5; g++) {
System.out.println(A + g + B);
w.findElement(By.xpath("//*[contains(text(),'Next')]")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("clicked this page" + g);
box2 = w.findElement(By.xpath("//*[@id='ires']"));
links2 = w.findElements(By.xpath("//*[@id='rso']/div/div/div/div/div/div/cite"));
l = g - 1;
System.out.println("Total links_page-> page: " + g + ":"
+ links2.size());
for (int h = 0; h < links2.size(); h++)
System.out.println("Listing:" + links2.get(h).getText());
System.out
.println("-----------------------------------------------");

}
}

}[/code:12f1isx6]


M
Replied on 19/01/2016

sir,

excercise c) says click all the links & see if links open up a window.

But since these are not links , is expectation to w.get(respective url) & see if text "page not found" pops up in the respective browser window?


M
Replied on 19/01/2016

yeah that would be fine.


M
Replied on 19/01/2016

Am getting exception:
org.openqa.selenium.WebDriverException: Session not found: 23def0a1-dd11-4f79-b562-e48503831d77
Command duration or timeout: 16 milliseconds
---------------------------------------------------------------------------------------------------------------

following is my code:

package p1;

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

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
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 google_c {
static WebElement box2;
static List<WebElement> links2;
static ProfilesIni prof = new ProfilesIni();
static FirefoxProfile p = prof.getProfile("Chaitra");


static WebDriver w=new FirefoxDriver(p);
public static void main(String[] args) {
// TODO Auto-generated method stub






w.get("https://www.google.com");
w.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
w.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
w.manage().window().maximize();
w.findElement(By.name("q")).sendKeys("types of kindles");
w.findElement(By.name("q")).sendKeys(Keys.ENTER);
//==========print links==1st page=========================

WebElement box=w.findElement(By.xpath("//*[@id='ires']"));
List<WebElement> links = w.findElements(By.xpath(".//*[@id='rso']/div/div/div/div/div/div/cite"));
System.out.println("Total links -> "+ links.size());
for(int i=0;i<links.size();i++)
System.out.println(links.get(i).getText());


//--------------click on every link of 1st page---------------------------

for(int i=0;i<links.size();i++)
{

try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(links.get(i).getText());
w.navigate().to(links.get(i).getText());
w.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
if(w.getPageSource().contains("Page Not Found"))
{
System.out.println("following link is broken"+links.get(i).getText());
w.navigate().back();
w=new FirefoxDriver();
}
else

{
//System.out.println("following link is workin fine"+links.get(i).getText());
w.navigate().back();
w.close();
w=new FirefoxDriver();
}




try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("clicked"+i+"th link");
}
}

/*

String A="//*[@id='nav']/tbody/tr/td[";
String B="]";
int l;
//==========print links==2 to 5 pages=========================
for(int g=2;g<=5;g++)
{
System.out.println(A+g+B);
w.findElement(By.xpath("//*[contains(text(),'Next')]")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
w.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
System.out.println("clicked this page"+g);
WebElement box2=w.findElement(By.xpath("//*[@id='ires']"));
List<WebElement> links2 = w.findElements(By.xpath("//*[@id='rso']/div/div/div/div/div/div/cite"));
l=g-1;
System.out.println("Total links_page-> page: "+g+":"+ links2.size());
for(int h=0;h<links2.size();h++)
System.out.println("Listing:"+links2.get(h).getText());
System.out.println("-----------------------------------------------");

}*/
}