0

I get id of a text which is in selected mode using Selenium Webdriver by using this code:

String requiredId = driver.FindElement(By.XPath("//option[@selected='selected' and .='Blue']/..")).GetAttribute("id");

How can i pass string getColourin place of Blue?

Thank you

by dukaan
  • 17
  • 6

1 Answers1

0

You can pass a string this way. Try below code

    string getColourin = "Red";
    String requiredId = driver.FindElement(By.XPath("//option[@selected='selected' and .='" + getColourin +"']/..")).GetAttribute("id");

OR

using string.Format

string xpathBefore = "//option[@selected='selected' and .='{0}']/..";
string getColourin = "Red";
string finalXpath = string.Format(xpathBefore, getColourin);

String requiredId = driver.FindElement(By.XPath(finalXpath)).GetAttribute("id");
Muzzamil
  • 2,823
  • 2
  • 11
  • 23