selenium - isElementPresent is very slow in case if element does not exist. -
i using below code check element on web page
private boolean iselementpresent(by by) { try { driver.findelement(by); return true; } catch (nosuchelementexception e) { return false; } catch (exception e) { return false; } }
i need check in program if particular region appears in result below
iselementpresent(by.xpath(".//*[@id='header']")));
if present function completes if above not present run long.
could 1 please me in resolving issue check can performed quickly?
here missing somethings why waiting if there not element. findelement wait element implicitly specified time. need set time 0 in method.
iselementpresent(webdriver driver,by by) { driver.manage().timeouts().implicitlywait(0, timeunit.seconds); try { driver.findelement(by); return true; } catch(nosuchelementexception e) { return false; } { driver.manage().timeouts().implicitlywait(30, timeunit.seconds); } }
there 4 important things going on here. in order:
1.setting implicity_wait 0 webdriver not implicitly wait.
2.returning true when element found.
3.catching nosuchelementexception , returning false when discover element not present instead of stopping test exception.
4.setting implicitly_wait 30 after action complete webdriver implicitly wait in future.
Comments
Post a Comment