c# - Selenium - DOM not refreshing after javascript has loaded element -
i looking following element:
<input id="login_input" value="" class="input_long " type="text" name="login" tabindex="1">
this element being loaded javascript after initial page loaded. code obtaining element:
iwebdriver _drv = new chromedriver(); _drv.navigate().gotourl("http://mysite.com"); system.threading.thread.sleep(2000); { try { _drv.findelement(by.id("login_input")).sendkeys("555567756756"); } catch (exception e) { console.writeline(e.message); } } while (true);
the error gives me saying a first chance exception of type 'openqa.selenium.nosuchelementexception' occurred in webdriver.dll
, when element visible on site.
what can obtain element?
update: changed code little bit:
iwebdriver _drv = new chromedriver(); _drv.url = "http://mysite.com"; //_drv.navigate().gotourl("http://mysite.com"); { try { waituntilpresent(by.id("login_input")).sendkeys("555567756756"); } catch (exception e) { console.writeline(e.message); } } while (true); iwebelement waituntilpresent(by element) { return new webdriverwait(_drv, timespan.fromseconds(10)).until(expectedconditions.elementexists(element)); }
the above gives me timeout exception, because _drv.pagesource
not being updated after javascript on page has loaded element.
try using explicit wait. -
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-waits
you getting error because findelement()
errors out if not present. it's there, code might executing before it's visible.
your code like
iwebdriver _drv = new chromedriver(); _drv.get("http://mysite.com"); // don't use navigate().gotourl(). // don't use waits.. webdriver has what's called "implicit waits" prevent having use sleep(). why don't see `selenium.waitforpagetoload()` anymore. webelement field = waituntilpresent(_drv.findelement(by.id("login_input"))); field.sendkeys("555567756756"); ... webelement waituntilpresent(webelement element) { return new webdriverwait(_drv, 10)) // might have casting here .until(expectedconditions.presenceofelementlocated(element)); }
Comments
Post a Comment