3

I have two links in pageA. when I click the 1st link it redirects to another page called pageB and do some certain jobs and returns back to pageA. From here it should again click to the 2nd link but instead it says page has reloaded and no cache available.

//List of all tickets
for(WebElement ticket: ticketList){
List<WebElement> ticketCells = ticket.findElements(By.tagName('td'));
  if(ticketCells.get(4).getText().equalIgnoreCase("Some Text")){
   ticketCells.get(2).click(); //Redirects to pageB
   .....
   do some job
   .......
   //Finally clicking on the 'SAVE & BACK' button which should return  to previous 
   //page and pick the 2nd ticket from the list of all tickets (1st for loop) 
   driver.findElement(By.id("save&back")).click();
  }
}

Here though it it is going back to previous page pageA but unable to pick the 2nd element from for loop for next operation.

Any thoughts on how to make it work.

12
  • did you need to navigate to the previous page in the same browser tab? Commented Jul 16, 2017 at 6:57
  • @santhoshkumar Yes, all these should happen to single browser tab. Commented Jul 16, 2017 at 7:00
  • driver.navigate().back(); will take you to the previous page Commented Jul 16, 2017 at 7:02
  • 1
    Don't add a link to the website like Guy said. It will change or vanish by time and make this question useless to others. Describe the page and the flow, possibly add screenshots or scribbles. Commented Jul 16, 2017 at 8:14
  • 2
    Note that the second interaction with ticket will probably (you haven't mentioned an error message) fail with a StaleElementReferenceException since you've altered the page contents due to navigation. When navigating back (currently it's unclear whether this exception or the navigation is your problem) the elements in that list can't be interacted with. You have to select them again after navigating and use a counter variable instead of an iterator (determine there are N rows and count from 0..N-1 and do ticketList.get(index)). Please add the exception you get. Commented Jul 16, 2017 at 8:24

3 Answers 3

3

I think that what you want is the equivalent to press the Back button of your browser, right?

If its this, try that:

webDriver.navigate().back();
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to navigate to back or previous page in java selenium browser. You can try

webDriver.navigate().back();

If you want to navigate to back or previous page in python selenium browser. You can try

driver.back();

If you want to navigate to back or previous page in JavaScript selenium browser. You can try

driver.execute_script("window.history.go(-1)");

Comments

0
@try-catch-finally explained it very clearly. The below code is exactly what you need to handle the error.

for(int i = 0; i <2 ;i++){
ticketList = driver.findElements(selector);
ticket = ticketList.get(i);
List<WebElement> ticketCells = ticket.findElements(By.tagName('td'));
  if(ticketCells.get(4).getText().equalIgnoreCase("Some Text")){
   ticketCells.get(2).click(); //Redirects to pageB
   .....
   do some job
   .......
   //Finally clicking on the 'SAVE & BACK' button which should return  to previous 
   //page and pick the 2nd ticket from the list of all tickets (1st for loop) 
   driver.findElement(By.id("save&back")).click();
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.