c# - Selenium.NoSuchElementException with dynamic tables -
please me problem!
at moment, scraping website using selenium firefox driver in c#. data on website dynamically filled tables cover data concerning future dates.
while structure of table same both future , past dates, tables being updated during selenium call throw "nosuchelementexception" concerning iwebelements there.
these relevant copied xpaths tables. 1 past date, on works fine, , 1 on future date, on exception thrown. can see, identical.
xpath 18052015
/html/body/div[1]/div/div[2]/div[5]/div[1]/div/div[1]/div[2]/div[1]/div[7]/div[1]/table/tbody/tr[1]/td[1]/div/a[2]
xpath 05022016
/html/body/div[1]/div/div[2]/div[5]/div[1]/div/div[1]/div[2]/div[1]/div[7]/div[1]/table/tbody/tr[1]/td[1]/div/a[2]
using findelements(by.xpath(...)) function, use 2 foreach loops go through highlighted tr's , td's in xpath text in a[2] header. in both cases, dom in firefox firebug seems identical in both cases. difference have observed between 2 tables fact every few seconds, 1 concerning future date updates values (also resetting table when looked @ via firebug). here have relevant piece of code, important comment.
foreach (var tr in table.findelements(by.xpath("div/table/tbody/tr"))) { foreach (var td in tr.findelements(by.xpath("td"))) { if(td.getattribute("innerhtml").contains("some stuff")) { // part reached, condition satisfied. > x relevant value, assigned proper value when error thrown, still throws exception. x = td.findelement(by.xpath("div/a[2]")).getattribute("href").split('/')[4]; bmid = getbookmakerid(bmname); } if(td.getattribute("class").contains("some other stuff")) { } }
has of had similar problems before , able solve them?
could add wait every steps call findelement? see example below:
iwait<iwebelement> wait = new defaultwait<iwebelement>(table); wait.timeout = timespan.fromseconds(5); wait.pollinginterval = timespan.frommilliseconds(300); locator = by.xpath("div/table/tbody/tr"); readonlycollection<iwebelement> rows; wait.until(e => e.findelements(locator).count > 0); rows = table.findelements(locator); foreach (var tr in rows) { wait = new defaultwait<iwebelement>(tr); wait.timeout = timespan.fromseconds(5); wait.pollinginterval = timespan.frommilliseconds(300); locator = by.xpath("td"); readonlycollection<iwebelement> cells; wait.until(e => e.findelements(locator).count > 0); cells = tr.findelements(locator); foreach (var td in cells) { if (td.getattribute("innerhtml").contains("some stuff")) { // part reached, condition satisfied. > x relevant value, assigned proper value when error thrown, still throws exception. wait = new defaultwait<iwebelement>(td); wait.timeout = timespan.fromseconds(5); wait.pollinginterval = timespan.frommilliseconds(300); locator = by.xpath("div/a[2]"); iwebelement link2; wait.until(e => e.findelements(locator).count > 0); try { link2 = td.findelement(locator); } catch (nosuchelementexception ex) { throw new nosuchelementexception("unable find element, locator: \"" + locator.tostring() + "\"."); } x = link2.getattribute("href").split('/')[4]; bmid = getbookmakerid(bmname); } if (td.getattribute("class").contains("some other stuff")) { } } }
if still error, can debug test in visual studio easily.
Comments
Post a Comment