Selenium(C#) - 如何获取元素属性的字符串

时间:2017-08-31 18:59:35

标签: c# html selenium attributes

有没有办法获取特定元素中所有属性的字符串?

例如,假设我的元素在网页中是以下内容:

<input id="SandBox1" checked="" class="float_right red white_stripes" value ="654288">

如果有可能从这里得到一个字符串变量,那么它的属性名称及其值都在一个字符串中?

1 个答案:

答案 0 :(得分:1)

下面的代码应该为你做。

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
var elem = driver.FindElement(By.Id("SandBox1"));

string attributes = (string)js.ExecuteScript("var re = /<[^ ]+([^>]+)/i; return re.exec(arguments[0].outerHtml)[1];", elem);

但是,如果属性在值

中也有>,则会出现这种情况