jquery - selenium is not clicking a link whereas on javascript, link is clicked flawlessly -
i using selenium webdriver click link throws nosuchelementexception. using xpath finds desired link when perform search in browser in inspect element tab, when execute below javascript in browser - console, link clicked gracefully,
javascript code:
var element = window.top.document.evaluate("//a[@id='clients']/i[@class='icon-chevron-down']" ,document, null, xpathresult.first_ordered_node_type, null ).singlenodevalue;if (element != null) { element.click(); } else { alert('not found'); }
but not sure why selenium throwing below exception, i've tried kinds of explicit wait before clicking.
org.openqa.selenium.nosuchelementexception: no such element (session info: chrome=42.0.2311.135) (driver info: chromedriver=2.9.248307,platform=mac os x 10.9.5 x86_64) (warning: server did not provide stacktrace information) command duration or timeout: 45.05 seconds documentation on error, please visit: http://seleniumhq.org/exceptions/no_such_element.html build info: version: 'unknown', revision: 'unknown', time: 'unknown' system info: host: 'vdo105.local', ip: '172.16.0.70', os.name: 'mac os x', os.arch: 'x86_64', os.version: '10.9.5', java.version: '1.7.0_75' session id: b9f85e7103119d4c7b7ff265d02187ca driver info: org.openqa.selenium.chrome.chromedriver capabilities [{platform=mac, acceptsslcerts=true, javascriptenabled=true, browsername=chrome, chrome={userdatadir=/var/folders/1v/pyj3pk396pb0z71_tlqcppgh0000gn/t/.org.chromium.chromium.tepz9p}, rotatable=false, locationcontextenabled=true, version=42.0.2311.135, takesheapsnapshot=true, cssselectorsenabled=true, databaseenabled=false, handlesalerts=true, browserconnectionenabled=false, webstorageenabled=true, nativeevents=true, applicationcacheenabled=false, takesscreenshot=true}]
here html code of link:
<a ng-show="sidebar.length" class="dropdown-toggle toggle-active" id="clients" href="javascript:void(0);"> <i class="icon-group"></i> <span class="menu-lv-1 ng-binding">clients</span> <i class="icon-chevron-down"></i> </a>
this happen when page populated javascripts. when new page appears selenium receives after has finished loading. if changed afterwards, selenium not have these changes.
it sounds youre problem.
for created special functions use instead of ones selenium provides. class contain webdriver instance.
public void click(final by){ act(by, 3, new callable<boolean>() { public boolean call() { wait.until(expectedconditions.refreshed(expectedconditions.visibilityofelementlocated(by))); driver.findelement(by).click(); logger.info("button "+stripby(by) + " clicked"); return boolean.true; // found } });}
function act special function retrying speficyfied action on element.
private void act(by by, int trylimit, boolean mode, callable<boolean> method){ logger.trace( "looking element: " + stripby(by) ); driver.manage().timeouts().implicitlywait( 5, timeunit.seconds ); boolean unfound = true; int tries = 0; while ( unfound && tries < trylimit ) { tries += 1; try { webdriverwait wait = new webdriverwait(driver, 500); wait.until(expectedconditions.refreshed(expectedconditions.visibilityofelementlocated(by))); wait.until(expectedconditions.refreshed(expectedconditions.elementtobeclickable(by))); unfound = !method.call(); // found it, negated since feel more intuitive if call method returns true success } catch ( staleelementreferenceexception ser ) { logger.error( "error: stale element exception. " + stripby(by)); unfound = true; } catch ( nosuchelementexception nse ) { logger.error( "error: no such element exception. " + stripby(by)+"\nerror: "+nse ); unfound = true; } catch ( exception e ) { logger.error( e.getmessage() ); } } driver.manage().timeouts().implicitlywait( constants.default_implicit_wait, timeunit.seconds ); if(unfound) assert.asserttrue(false,"failed locate element locator " + stripby(by));}
Comments
Post a Comment