Home > OS >  How to store a WebElement without having the driver to execute it?
How to store a WebElement without having the driver to execute it?

Time:09-29

I want to store an Xpath in a variable without having the Webdriver to execute it.

I want to store it in a variable so that I can use it in an if else statement

var Resetbtn = driver.FindElement(By.XPath("//button[text()='Reset']"));
var Exebtn = driver.FindElement(By.XPath("//button[text()='Exe']"));

if I remove driver I get the following error "The name 'FindElement' does not exist in the current context"

CodePudding user response:

FindElement is a method from IWebDriver. You can store it in a variable, but without parameters. Instead you can store the By as a variable and send it to FindElement later

By resetBtnBy = By.XPath("//button[text()='Reset']");

var Resetbtn = driver.FindElement(resetBtnBy);

CodePudding user response:

You can do something like this :

By reset_xpath = By.XPath("//button[text()='Reset']");

and can use it like this :

 var Resetbtn = driver.FindElement(reset_xpath);
  • Related