Suggest an adequate way to loop each item search and click on it. (Why do you need this, please do not ask. In this case, this is the only way) With goto, everything would be beautiful, but alas .. Displaying each search for an element into a separate method is also not beautiful. I have a couple of dozen of such elements, and if each of them is wrapped with code for a method, then everything looks bad.

public static void findElement() { WebElement element = driver.findElement(By.id("")); element.click(); WebElement element1 = driver.findElement(By.cssSelector("label"); element1.click(); WebElement element2 = driver.findElement(By.id("")); element2.sendKeys(elementsOfTheApplication.getC()); try { goto: WebElement element3 = driver.findElement(By.cssSelector("s']")); element3.click(); } catch (Exception e) { //переход на goto e.printStackTrace(); } WebElement element4 = driver.findElement(By.cssSelector("]")); element4.click(); WebElement element5 = driver.findElement(By.cssSelector("l-']")); element5.click(); } 
  • 3
  • But, I do not believe that there are no other solutions. Most likely you are doing something wrong. - test123
  • Try to make a universal method with callbacks. So you simplify the design and get rid of the same code. In the body of callback, you can specify what you need to do - click, or send a keystroke. Next, you can implement a couple of trivial callbacks, and that's it, half the code is automated, and the body of your function will consist of clear and simple commands - test123
  • one
    "Displaying each item search in a separate method is also not beautiful." - all two methods work out. One for find + click, another for find + sendKeys. And both parameters will have what you pass to findElement. And no kolbekov is not needed, as you wrote above. Goto is a hell of a crap, it’s very old to see "everything would be beautiful with goto" also in comparison with functions. There at least one option was offered to you, I can still offer something in the spirit of while (isSuccess) - it will be clearer. And the names element1, element2 - the second place in the nightmare after goto. Two worst practices in one question - hard) - Uraty
  • And there is an even more beautiful option, if you put it into a function, you can make pastebin.com/5uazghi3 very nicely through recursion. But with this neatly, you can get StackOverwlowException - Uraty

1 answer 1

After reading carefully your comments wrote this solution: three static methods in a class created as an Object Model

 private static void FindClick(By by) { for (int i = 0; i < 3; i++) { try { WebElement element = driver.findElement(by); element.click(); break; } catch (Exception e) { e.printStackTrace(); } } } private static void FindSend_keys(By by, String key) { for (int i = 0; i < 3; i++) { try { WebElement element = driver.findElement(by); element.sendKeys(key); break; } catch (Exception e) { e.printStackTrace(); } } } private static void FindClearSendKeys(By by, String key) { for (int i = 0; i < 3; i++) { try { WebElement element = driver.findElement(by); element.clear(); element.sendKeys(key); break; } catch (Exception e) { e.printStackTrace(); } } }