How to pass string to an XPath containing text?

泄露秘密 提交于 2020-02-21 07:03:20

问题


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


回答1:


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");


来源:https://stackoverflow.com/questions/60023252/how-to-pass-string-to-an-xpath-containing-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!