C# 如何将字符串传递给包含文本的XPath?

C# 如何将字符串传递给包含文本的XPath?,c#,winforms,selenium,selenium-webdriver,xpath,C#,Winforms,Selenium,Selenium Webdriver,Xpath,通过使用以下代码,我使用Selenium Webdriver获取处于选定模式的文本id: String requiredId=driver.FindElementBy.XPath//option[@selected='selected'and.='Blue']/…GetAttributeid; 我如何传递字符串来代替蓝色 谢谢您可以用这种方式传递字符串。试试下面的代码 string getColourin = "Red"; String requiredId = driver.

通过使用以下代码,我使用Selenium Webdriver获取处于选定模式的文本id:

String requiredId=driver.FindElementBy.XPath//option[@selected='selected'and.='Blue']/…GetAttributeid;

我如何传递字符串来代替蓝色


谢谢

您可以用这种方式传递字符串。试试下面的代码

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

使用string.Format


$/选项[@selected='selected'和.='{getcolor}']/。。我应该帮助你。嗨,兄弟,你能告诉我这个答案吗@Muzzamil@bydukaan我回答了很久。请点击thereHi bro,请帮我解决这个问题,兄弟,等待你的帮助,上面的链接@MuzzamilAbove链接是不可点击的
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");