Module 17 - Exercises 2(b) | Selenium Forum
M
Posted on 20/05/2016
Hi, I am trying to print names of all result links on first 5 pages.. for loop is working fine for 1st page and 2nd page, then instead of going to 3rd page it is going to 1st page,2nd page and 1st page. Can you please tell me where am I going wrong.

for(int j=3;j<7;j++)
{
Thread.sleep(4000);
WebElement box=driver.findElement(By.xpath("//*[@id='rso']"));
List<WebElement> links= box.findElements(By.tagName("cite"));
System.out.println("No.of links in the page are : " +links.size());
for(int i=1;i<links.size();i++)
{
String linkname=links.get(i).getText();
System.out.println(linkname);
}
driver.findElement(By.xpath("//*[@id='nav']/tbody/tr/td['"+j+"']/a")).click();
}
/* Page 2 : //*[@id='nav']/tbody/tr/td[3]/a
* Page 3 : //*[@id='nav']/tbody/tr/td[4]/a
* Page 4 : //*[@id='nav']/tbody/tr/td[5]/a
* Page 5 : //*[@id='nav']/tbody/tr/td[6]/a
*/

M
Replied on 21/05/2016

please provide the entire code.


M
Replied on 23/05/2016

public class GoogleSearch_b {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

driver.get("http://google.com");

driver.findElement(By.name("q")).sendKeys("selenium");
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
for(int j=3;j<7;j++)
{
Thread.sleep(4000);
WebElement box=driver.findElement(By.xpath("//*[@id='rso']"));
List<WebElement> links= box.findElements(By.tagName("cite"));
System.out.println("No.of links in the page are : " +links.size());

for(int i=1;i<links.size();i++)
{
String linkname=links.get(i).getText();
System.out.println(linkname);
}

driver.findElement(By.xpath("//*[@id='nav']/tbody/tr/td['"+j+"']/a")).click();

}
/* Page 2 : //*[@id='nav']/tbody/tr/td[3]/a
* Page 3 : //*[@id='nav']/tbody/tr/td[4]/a
* Page 4 : //*[@id='nav']/tbody/tr/td[5]/a
* Page 5 : //*[@id='nav']/tbody/tr/td[6]/a
*/


M
Replied on 24/05/2016

[quote:10lx0utr]WebElement box=driver.findElement(By.xpath("//*[@id='rso']"));
List<WebElement> links= box.findElements(By.tagName("cite"));
System.out.println("No.of links in the page are : " +links.size());
[/quote:10lx0utr]

these xpaths are not working on second page and its giving me an error. please try to find better xpaths.