在XPath中使用变量

-1 c# selenium xpath xpath-1.0 selenium-webdriver

这是有效的,但我想使用文本Group 1作为变量.

代码试用:

[FindsBy(How = How.XPath, Using = "(.//*[normalize-space(text()) and normalize-space(.)= 'Group 1'])[1]/ancestor::app-organization//*[normalize-space(text()) and normalize-space(.)='Create a new board...']/following::input[1]")]
public IWebElement BoardNameInputField { get; set; }
Run Code Online (Sandbox Code Playgroud)

我试过这个但没有成功:

string boardName = "Group 1";

[FindsBy(How = How.XPath, Using = "(.//*[normalize-space(text()) and normalize-space(.)='${boardName}'])[1]/ancestor::app-organization//*[normalize-space(text()) and normalize-space(.)='Create a new board...']")]
public IWebElement CreateNewBoard { get; set; }
Run Code Online (Sandbox Code Playgroud)

是否有可能做到这一点?

Guy*_*Guy 5

在字符串插值中,$应该在字符串之前,而不是变量

[FindsBy(How = How.XPath, Using = $"(.//*[normalize-space(text()) and normalize-space(.)='{boardName}'])[1]/ancestor::app-organization//*[normalize-space(text()) and normalize-space(.)='Create a new board...']")]
Run Code Online (Sandbox Code Playgroud)

另外,boardName需要在方法范围外使用静态

static string boardName = "Group 1";
Run Code Online (Sandbox Code Playgroud)