c# - IWebElement object needs to be created each time to find a new element on a page? -


iwebelement abc = driver.findelement(by.id("txtusername")); abc.sendkeys("test"); iwebelement abc1 = driver.findelement(by.id("txtpassword")); abc1.sendkeys("test123"); iwebelement abc2 = driver.findelement(by.id("buttonsignin")); abc2.click(); 

do need explicitly create new object each time find element on same page?

can create iwebelement once? selenium 1?

selenium.type("test"); selenium.click(); 

what makes think that's creating new object?

it's declaring new variable, that's not same thing... , it's important understand difference between them.

i can't see reason why couldn't reuse single variable if wanted to... i'd try use different variable names represent different elements finding:

iwebelement usernameinput = driver.findelement(by.id("txtusername")); usernameinput.sendkeys("test");  iwebelement passwordinput = driver.findelement(by.id("txtpassword")); passwordinput.sendkeys("test123");  iwebelement signinbutton = driver.findelement(by.id("buttonsignin")); signinbutton.click(); 

that makes clearer action i'm trying take, imo. of course, if you're using "find element, send keys" pattern regularly, might want write convenience method, call:

entertext("txtusername", "test"); entertext("txtpassword", "test123"); submitform("buttonsignin"); 

Comments