Module 17- Exercise | Selenium Forum
M
Posted on 03/07/2016
Hi,
In Exercise 2C) "Click on each and every link on first 5 pages and check if next page is opening". It is working fine till the page No.4 but after that code throw the bellow error.

NOTE:- Project attached. Please run below code on your machine and tell me where i am wrong.


Code :-

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

import org.omg.CORBA.PUBLIC_MEMBER;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sam_PrintName_Google_First_FivePage {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {

//System.setProperty("webdriver.chrome.driver", "D://chromedriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://google.co.in");
driver.findElement(By.xpath("//input[@id='lst-ib']")).sendKeys("hello pratibha");
driver.findElement(By.name("btnG")).click();
List<WebElement> allLinks= driver.findElements(By.xpath("//div[@id='rso']/div/div/div/h3/a"));
WebElement lastFriend = allLinks.get(allLinks.size()-1);

int y= lastFriend.getLocation().y;
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0, "+y+")");

// Code for Paging for a limited number of pages.
for (int i=3;i<=7;i++){

driver.findElement(By.xpath("//*[@id='nav']/tbody/tr/td["+i+"]")).click();
Thread.sleep(3000L);

// Code for Scrolldown using javascriptExecuter
allLinks= driver.findElements(By.xpath("//div[@id='rso']/div/div/div/h3/a"));
lastFriend = allLinks.get(allLinks.size()-1);
y= lastFriend.getLocation().y;
js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0, "+y+")");
// Print all the names links on the page.


for(int count =0;count<allLinks.size();count++){

System.out.println("Name of the Links are -- >" +allLinks.get(count).getText());
allLinks.get(count).click();
Thread.sleep(3000L);
driver.navigate().back();


driver.findElement(By.xpath("//*[@id='nav']/tbody/tr/td["+i+"]")).click();
Thread.sleep(3000L);

// Code for Scrolldown using javascriptExecuter
allLinks= driver.findElements(By.xpath("//div[@id='rso']/div/div/div/h3/a"));
lastFriend = allLinks.get(allLinks.size()-1);
y= lastFriend.getLocation().y;
js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0, "+y+")");


}

}

}

// Using While loop to click on all the pages and navigate to next page.

/*int count =3;
while(isElementPresent("//*[@id='nav']/tbody/tr/td["+count+"]")){
driver.findElement(By.xpath("//*[@id='nav']/tbody/tr/td["+count+"]")).click();
count++;

Thread.sleep(3000L);
allLinks= driver.findElements(By.xpath("//div[@id='rso']/div/div/div/h3/a"));
lastFriend = allLinks.get(allLinks.size()-1);

y= lastFriend.getLocation().y;

js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0, "+y+")");

}
*/

public static boolean isElementPresent(String xpathExp)
{

int count = driver.findElements(By.xpath(xpathExp)).size();
if(count==0)
return false;
else
return true;
}


}

M
Replied on 03/07/2016

Any Update????


M
Replied on 04/07/2016

there must be no such element.

i don't see anything wrong with your code.