我使用以下语法来评估运行时中的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; }
如何解决问题?
Using
的字符串在编译时求值,因此它必须是常量。因此,不可能在FindsBy
中使用变量。你会得到错误
属性参数必须是属性参数类型的常量表达式、typeof表达式或数组创建表达式
如果要使用变量定位元素,则需要在使用FindElement
时定位元素
IWebElement txt_RefPriority = driver.FindElement(By.XPath("//input[@id='"+Context.LoginName+"']"));