Doubt in Selenium WebDriver | Selenium Forum
M
Posted on 29/02/2016
As the Example Explained by Ashish Sir in Module 13, i wrote the following program to click on a web page only in specific area

[b:3c0wl3b9][u:3c0wl3b9]Program:[/u:3c0wl3b9][/b:3c0wl3b9]

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;


public class Rediff_1 {


public static void main(String[] args) {

WebDriver dr = new FirefoxDriver();
dr.get("http://in.rediff.com/");
dr.manage().window().maximize();
dr.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement shop = dr.findElement(By.xpath("//*[@id='homewrapper']/div[5]/a[7]/div/u"));[b:3c0wl3b9][u:3c0wl3b9] //In this line i am finding the shopping link in rediff webpage[/u:3c0wl3b9][/b:3c0wl3b9]
shop.click();
WebElement left_box = dr.findElement(By.xpath("//*[@id='popular_cat']"));[b:3c0wl3b9][u:3c0wl3b9]// here i am finding the shopping links which was displayed in left part of webpage[/u:3c0wl3b9][/b:3c0wl3b9]
List <WebElement> shop_links = left_box.findElements(By.tagName("a"));

System.out.println(shop_links.size());

for(int i=0;i<shop_links.size();i++){
System.out.println(shop_links.get(i).getText());
shop_links.get(i).click();
System.out.println(dr.getTitle());//I have printed upto first link and first title here

[b:3c0wl3b9][u:3c0wl3b9]// I repeated the below code which was written in top as we lost the web page memory in the cache[/u:3c0wl3b9][/b:3c0wl3b9]

dr.get("http://in.rediff.com/");
dr.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement shop = dr.findElement(By.xpath("//*[@id='homewrapper']/div[5]/a[7]/div/u"));
shop.click();
WebElement left_box = dr.findElement(By.xpath("//*[@id='popular_cat']"));
List <WebElement> shop_links = left_box.findElements(By.tagName("a"));
}


}

}

But the problem i am facing is when i repeated the code again in the For Loop, it is giving me a warning like Rename the web elements like shop, left_box, shop_links

Please suggest me how to proceed further on this one

Please find the attached screenshot for the Program

M
Replied on 29/02/2016

Hello Team can anyone help me out on this, I am not able to figure out why the error is coming

Kindly Help me


M
Replied on 29/02/2016

i'll reply to you by the end of the day.


M
Replied on 01/03/2016

sorry for the delay

use it like this

[code:4svow8h0]package com.sample;

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;

public class Rediff_1 {

public static void main(String[] args) {

WebDriver dr = new FirefoxDriver();
dr.get("http://in.rediff.com/");
dr.manage().window().maximize();
dr.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement shop = dr.findElement(By
.xpath("//*[@id='homewrapper']/div[5]/a[7]/div/u")); // In this

shop.click();
WebElement left_box = dr
.findElement(By.xpath("//*[@id='popular_cat']"));// here i am

List<WebElement> shop_links = left_box.findElements(By.tagName("a"));

System.out.println(shop_links.size());

for (int i = 0; i < shop_links.size(); i++) {
System.out.println(shop_links.get(i).getText());
shop_links.get(i).click();
System.out.println(dr.getTitle());// I have printed upto first link
// and first title here

// I repeated the below code which was written in top as we lost the
// web page memory in the cache

dr.get("http://in.rediff.com/");
dr.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
shop = dr.findElement(By
.xpath("//*[@id='homewrapper']/div[5]/a[7]/div/u"));
shop.click();
left_box = dr.findElement(By.xpath("//*[@id='popular_cat']"));
shop_links = left_box.findElements(By.tagName("a"));
}

}

}
[/code:4svow8h0]