How do I find element in page number 4 looping trough 3 pages.

74 views
Skip to first unread message

Mike

unread,
Apr 15, 2015, 3:30:05 PM4/15/15
to seleniu...@googlegroups.com
Hi
I am trying to find element in page that you have to go trough 3 pages to get to it 
If I navigate page by page I can find the element by keep clicking next button but If I add while loop it get to the last page and stop,

here is the page:
<div class="guestForm">
<input id="reservations0.guests0.id" type="hidden" value="0" name="reservations[0].guests[0].id">
<br>
<div class="form-border">
<div class="field-col left">
<div class="field-row ">
<input id="reservations0.guests0.firstName" class="field err_fld" type="text" maxlength="40" value="" name="reservations[0].guests[0].firstName">
</div>

Here is my function:
public void checkForElementExist() {
// driver.findElement(By.xpath("//*[@id='reservations0.guests0.firstName"));
WebElement x = driver.findElement(By.xpath("//*[@id='reservations0.guests0.firstName"));
while (!(x.equals("reservations0.guests0.firstName"))){
System.out.println("Didn't Find it +++> " );
driver.findElement(By.xpath("//*[@id='search']/a")).click();
}if ((x.equals("reservations0.guests0.firstName"))){
System.out.println("Find it$$$> " + x);
}else
return;

}

Thanks

David Lai

unread,
Apr 15, 2015, 5:28:44 PM4/15/15
to seleniu...@googlegroups.com
Are you talking about page pages, or pages in a paginated view (ajax)?

A good way to handle paginated views, is to use the a StaleElement check, to use to wait for the old page contents to be unloaded.

while( some condition ) {
  if( element you're looking for not found) {
    IWebelement old = //find some element on the page;

    //click next page button.

   if( element you're looking for exist) break;

    // call wait wrapper to wait for 'old' element to become stale.    
  }

}

Mike

unread,
Apr 15, 2015, 10:26:42 PM4/15/15
to seleniu...@googlegroups.com
Hi
Thanks David.
Just regular pages.in page one i check for input field that has id='reservations0.guests0.firstName'
I know is not in the 1st page and I click on button which take you to page 2 than 3 than 4 where the element exist.
If I just use click on next 3 times I can find the element but if I want to use the while loop the loop doesn't start for me.
The reason why I want to use the while loop because the number of pages changes.
Thanks

Kavan Sheth

unread,
Apr 16, 2015, 2:02:51 PM4/16/15
to seleniu...@googlegroups.com
Hi Mike,

Not getting your code at all, check if any of the below applies.
1. if element is not found at first place it will throw NoSuchElement Exception , if you are talking about text than might be ok.
2. why are you directly comparing x(a WebElement) with "reservations0.guests0.firstName"? Any way , if you are checking text for that element use .getText()
3. in while loop, where you are updating(refreshing) value of your variable(WebElement) x after clicking on search?
    because of it is a page refresh , you need to get value again.

use print statements to understand behavior.

I hope this will help,

Thanks,
Kavan Sheth

Pavan Turlapati

unread,
Apr 17, 2015, 3:54:28 AM4/17/15
to seleniu...@googlegroups.com
I guess you can do something like this:

1. Add Page1,Page2, Page3 links can be added to an IList<IWebElement>.
2. Declare a boolean variable (objNotFound) with default value as true.
3. Have a While now  (while(objNotFound))
4. In the While have a ForEach of the above step 1 created links
5. In the Foreach 
        Click on the First(i) Page
        Check element existence
        If Object found Return true
        else loop
        

IList<IWebelement> pagesList=driver.GetElements(By.XPath(""));
bool objNotFound=true;
while(objNotFound)
{
  foreach(IWebElement ele in pagesList)
  {
   try
   {
      IWeblement searchElement=driver.getElement(By.XPath(""));
      if (searchElement!=null)
      {
       objNotFound=false;
       return searchElement;
      }  
   }
  catch // Obj Not Found
  {
    objNotFound=true;
   }
  } //ForEach
}//While












On Thursday, April 16, 2015 at 1:00:05 AM UTC+5:30, Mike wrote:

Mike

unread,
May 6, 2015, 11:00:21 AM5/6/15
to seleniu...@googlegroups.com

Thanks
But that didn't work either.
still can't find solution.
Reply all
Reply to author
Forward
0 new messages