Incrementing Xpath in loop

2,230 views
Skip to first unread message

Vinay Kumar

unread,
Aug 14, 2012, 2:34:03 AM8/14/12
to webd...@googlegroups.com
Hi. .

my scenario is something like. . i have given While(true).inside while loop i have given try and catch block.
in try block i have incremented i value in xpath i.e

driver.findElement(By.xpath("//html/body/div[2]/div[2]/div[2]/div[2]/div[1]/div/div[3]/div/div["+i+"]/div[2]/div[1]/div")).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                System.out.println(driver.findElement(By.xpath("//html/body/div[7]/div/div/div/div/div[1]/div/div[2]/ul/li[3]/div")).getText());
                driver.findElement(By.xpath("//html/body/div[7]/div/div/div/div/div[1]/div/div[3]/span[2]")).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                i++;

it ll search till last element and come to catch block.in next iteration again i value should start from 1 and j value has to be incremented for
click command. its not incrementing j value for me.
//html/body/div[2]/div[2]/div[2]/div[2]/div[1]/div/div[3]/div/div["+i+"]/div[2]/div[1]/div")).click();

                                                     in place of div[1] i need to increment.
can any1 help me out to perform this??




Smita Sinha

unread,
Aug 14, 2012, 2:57:11 AM8/14/12
to webd...@googlegroups.com
Give us the complete code.
I am not able to find where you have declared j and how are you incrementing it.

-Smita






--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/3X6P4S5UhFwJ.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Vinay Kumar

unread,
Aug 14, 2012, 3:04:27 AM8/14/12
to webd...@googlegroups.com
int i=1;j=1;

while(true)
        {
                
              try
                {
                driver.findElement(By.xpath("//html/body/div[2]/div[2]/div[2]/div[2]/div[//This is considered as j.this has to be incremented next time to 2 after coming out of while]/div/div[3] /div/div["+i+"]/div[2]/div[1]/div")).click();

                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                System.out.println(driver.findElement(By.xpath("//html/body/div[7]/div/div/div/div/div[1]/div/div[2]/ul/li[3]/div")).getText());
                driver.findElement(By.xpath("//html/body/div[7]/div/div/div/div/div[1]/div/div[3]/span[2]")).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                i++;
                
                }
              
              catch(Exception e)
                {
                 break;
                }
            
        }  *///END OF WHILE LOOP 

Smita Sinha

unread,
Aug 14, 2012, 3:11:37 AM8/14/12
to webd...@googlegroups.com
Vinay,

First, I would suggest you to revisit your xpaths.
Second why don`t you use two for loops for the kind of requirement you have.
Something like
for(j= 0;j<"Your condition for j loop";j++){
    for(i= 0 ;i<"your condition for i loop";i++){
        try
                {
                driver.findElement(By.xpath("//html/body/div[2]/div[2]/div[2]/div[2]/div["+j+"]/div/div[3] /div/div["+i+"]/div[2]/div[1]/div")).click();

                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
                System.out.println(driver.findElement(By.xpath("//html/body/div[7]/div/div/div/div/div[1]/div/div[2]/ul/li[3]/div")).getText());
                driver.findElement(By.xpath("//html/body/div[7]/div/div/div/div/div[1]/div/div[3]/span[2]")).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
                i++;
                
                }
              
              catch(Exception e)
                {
                 break;
                }
             
        }  *///END OF WHILE LOOP  
    }
}

Like this once it has executed whole of i it will increment j .

-Smita

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/xGpRxBxtGXEJ.

Smita Sinha

unread,
Aug 14, 2012, 3:13:13 AM8/14/12
to webd...@googlegroups.com
Remove the comment and the i++ thing from the code.

-Smita

Vinay Kumar

unread,
Aug 14, 2012, 3:19:00 AM8/14/12
to webd...@googlegroups.com
I need to increment j 8 times and i value ll keep locating elements untill last element is found.
once last element is found j should become 2 and i value again from 1 it should start

Smita Sinha

unread,
Aug 14, 2012, 3:27:08 AM8/14/12
to webd...@googlegroups.com
Your j loop will run 0to 8.
For i loop you need to understand what do you mean by last element.
is there a static value that you can put or the xpath will not be found when it reaches the last element etc.
conclude for i and put the condition in the "i" for loop.

This should work.

-Smita


On Tue, Aug 14, 2012 at 12:49 PM, Vinay Kumar <vinaykuma...@gmail.com> wrote:
I need to increment j 8 times and i value ll keep locating elements untill last element is found.
once last element is found j should become 2 and i value again from 1 it should start

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/PBdj9SQ3sNIJ.

Vinay Kumar

unread,
Aug 14, 2012, 3:33:26 AM8/14/12
to webd...@googlegroups.com
I HAVE A CATEGORY CALLED AS VIDEOS.

1ST VIDEO I VALUE IS 1
2ND VIDEO I VALUE IS 2 AND SO ON.

SO UNTILL LAST VIDEO IS PRESENT "I" VALUE GETS INCREMENTED.ONCE LAST "I" VALUE IS CAPTURED
J SHOULD INCREMENT TO 2 AND I VALUE STARTS AGAIN FROM 1 FOR THE NEXT CATEGORY.

darrell

unread,
Aug 14, 2012, 10:38:08 AM8/14/12
to webdriver
int i, j;
int max1 = 11, max2 = 8;
for(i = 1; i <= max1; i++) {
for(j = 1; j <= max2; j++) {
String myXPath = "/html/body/div[2]/div[2]/div[2]/div[2]/
div["
+ j + "]/div/div[3]/div/div[" + i + "]/div[2]/div[1]/
div";
try {
driver.findElement(By.xpath(myXPath)).click();
// put the rest of your code here
} catch(Exception e) {
}
}
}

In this example, I have set it so that i will range from 1 to 11
(max1) and j will range from 1 to 8 (max2). If you want to change the
maximum value for i, change the max1 = 11. If you want to change the
maximum value for j, change the max2 = 8.

There are issues with this code. For example, I never catch Exception.
If I am expecting NoSuchElementException then I'll catch
NoSuchElementException. If you catch Exception you might be catching
an exception you are not aware of and not handling correctly. This
leads to things every tester has seen. You get a log message that says
"code failed because of XXX." but when you investigate you find out it
failed for reasons completely different from XXX and there is no stack
trace to help you debug it.

Also, since we don't have the HTML you are trying to automate, I
wonder if using a nest for loop is the best solution or can you create
a single XPath, findElements then an iterator to go through the list,
e.g.

String mySingleXPath = "???";
List<WebElement> elements =
driver.findElements(By.xpath(mySingleXPath));
Iterator<WebElement> i = elements.iterator();
while(i.hasNext()) {
WebElement element = i.next();
element.click();
}

Lot cleaner and easier for a Java programmer to read.


Darrell
Reply all
Reply to author
Forward
0 new messages