c# - Why does Selenium not find an array-indexed XPath-based element when using Page Factory? -


i have pom (page object model) has following declaration:

public class mypom {  [findsby(how=how.xpath, using="(//textarea)[0]")]  private iwebelement questiondescription;   //this fails in selenium, successful in chrome-console:  [findsby(how=how.xpath, using="(//input[@class='cso-num'])[0]")]  private iwebelement questionscore;   public mypom(iwebdriver driver)  {      pagefactory.initelements(driver, this)  }  } 

on chrome console, $x("//textarea")[0] query fires fine. however, each of xpath selectors has such "array based indexing" results in nosuchelementexception. i'm not sure problem is. every validation of xpath, outside of selenium seems return valid html dom node, not selenium.

i added explicit pause, prior finding element on page follows no avail:

webdriverwait wait = new webdriverwait(_driver, timespan.fromseconds(5)); 

however, if following in method, works fine:

iwebelement questiondescription = getdriver().findelements(by.xpath(".//textarea"))[0]; questiondescription.clear(); questiondescription.sendkeys(description); 

is there limitation using array-based indexing when using xpath , pagefactory?

reason using xpath: of html autogenerated underlying framework developers have no access or control on (rather unfortunate). best way have reliable , non-brittle tests use xpaths.

update/solution: dumbest gotcha ever! xpath indexes 1 , not 0! however, before delete question, answer posted. i'm leaving resolution in place of posting separate answer.

when had first set selenium tests had similar issue. try using like:

driver.manage().timeouts().implicitlywait(5, timeunit.seconds); 

it should go after initialize driver. tried doing seperate waits each time loaded page didn't work me either. 5 seconds seems enough time platform developing for, can adjusted needed.

note code snippet may different in c#. came java program.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -