如何使用How.find评估xpath中的变量?

时间:2016-04-12 06:08:48

标签: xml selenium xpath selenium-webdriver

我使用以下语法在运行时评估Xpath

[FindsBy(How = How.XPath, Using = "//input[@id='icombobox_Text_cboRefPriority']")]
public IWebElement txt_RefPriority { get; set; }

以上工作完美,但是当我需要使用变量选择xpath时它不起作用。

例如:

Context.LoginName => will read a value from a XL/XML which can change dnamically

[FindsBy(How = How.XPath, Using = "//input[@id='"+Context.LoginName+"']")]
public IWebElement txt_RefPriority { get; set; }

如何解决问题?

1 个答案:

答案 0 :(得分:0)

在{em> compile 时间内评估Using的字符串,因此它必须是常量。因此,无法在FindsBy中使用变量。你会收到错误

  

属性参数必须是属性参数类型

的常量表达式,typeof表达式或数组创建表达式

如果要使用变量找到元素,则需要在使用FindElement

时使用该元素时找到该元素
IWebElement txt_RefPriority = driver.FindElement(By.XPath("//input[@id='"+Context.LoginName+"']"));
相关问题