0

I want to exit loop if the string give an error, I cant check if it's null.

The loop take 2 scripts and take all the cells, in the end the compiler give an error.

How i can exit before calculating the last string(the error time) ?

Thanks

String script3 = "return document.getElementsByTagName('g')[" + f9 + "].textContent";
n = (String) ((JavascriptExecutor) driver).executeScript(script3, hiddenDiv); 

n takes all the tags(g), at the end it's give an error not null, so i cant stop the loop.

I want to stop the loop before the error.

I do this to count the tags.

JeffC
  • 22,180
  • 5
  • 32
  • 55
Mind5
  • 27
  • 7

3 Answers3

1

You can get all the g tags using findElements method

List<WebElement> gTags = driver.findElements(By.tagName("g"));

And to get the number of g tags

int size = gTags.size();
Guy
  • 46,488
  • 10
  • 44
  • 88
0

use try catch for catching the error and breaking the loop

for(...)
{
   try
   {
      //your javascript code HERE
   }
   catch(Exception ex)
   {
      string exMessage = getMessage();
      break;
   }
}
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43
0

There are several ways to do this. I would do it like the below. Basically you use Javascript to grab all the g tags, loop through them collecting their .textContent, and then return that List to your Java code. You can then loop through the List in Java and do whatever.

String script3 = "var strings = []; var g = document.getElementsByTagName('g'); for (var i = 0; i < g.length; i++) { strings.push(g[i].textContent); } return strings;";
List<String> s = (List<String>) ((JavascriptExecutor) driver).executeScript(script3);
System.out.println(s);
JeffC
  • 22,180
  • 5
  • 32
  • 55