Java Selenium WebDriver - correct exception handling techniques when unable to locate window -
i'm using java , selenium automate test cases. involves loading single page search results , iterating on each 100-1000 links on single page. setting test config check 100 results okay, higher , @ point nosuchwindowexception thrown(subclass of webdriverexception). happens when switch the parent handle newly opened window handle.
i've written try-catch statement inside while loop catch exception , retry procedure... however, whatever try, selenium not play nice, , code execution abruptly ends.. :( here code:
boolean completed = false; do{ try{ //click search result driver.findelement(by.xpath("my xpath string")).click(); //switch new window for(string winhandle: driver.getwindowhandles()){ driver.switchto().window(winhandle); } //for our test need save source source = driver.getpagesource(); //close popup window , and switch parent handle driver.close(); driver.switchto().window(parenthandle); completed = true; }catch(webdriverexception ex){ system.out.println("something went wrong while switching windows... retrying"); driver.close(); driver.switchto().window(parenthandle); } }while(!completed);
i've experimented various approaches when exception caught. example, tried saving parent url, using driver.quit() followed trying restart driver. however, selenium complained wasn't able start firefox driver after calling driver.quit()...
any ideas how better handle catch part of code?
okay, i've found solution comments left tala.
i removed following statements catch block:
driver.close(); driver.switchto().window(parenthandle);
now the catch block not generating separate uncaught exception , loop correctly work retrying previous code...
Comments
Post a Comment