让所有的属性从硒的webdriver的IWebElement [英] Getting ALL attributes from an IWebElement with Selenium WebDriver

查看:1366
本文介绍了让所有的属性从硒的webdriver的IWebElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要提取硒DOM中的一些信息。我使用的是C#的webdriver。



纵观IWebElement界面,您可以方便地提取一个给定的属性。不过,我想提取一个元素的所有属性,不知道之前的手他们的名字。



有必须这样做的一些简单的方法,因为有一种方法。为获得一个属性的值,如果你知道它的名字



一个例子:

 <按钮的id =myButton的NG点击=布拉布拉()NG-秀=showMyButton
部分,其他的属性=foo.bar​​/>



IWebElement元= driver.FindElement(By.Id(myButton的));
&字典LT;字符串,字符串>属性=新词典与LT;字符串,字符串>();
// ???????
//利润。



希望我失去了一些东西明显。



在此先感谢!


解决方案

.attributes 在JavaScript属性将返回所有给定的元素具有属性的数组和它的价值。



所以,你需要做的是先获得驱动程序有运行JavaScript的能力:

  IJavascriptExecutor javascriptDriver =(IJavaScriptExecutor)驱动程序; 



<$ P:

现在,通过执行它$ p> 词典<字符串对象>属性= javascriptDriver.ExecuteScript(VAR项目= {};对于(指数= 0;指数 - LT;参数[0] .attributes.length ++指数){项目[参数[0] .attributes [指数]。名称] =参数[0] .attributes [指数] .value的};返回的物品;元素)作为字典<字符串对象> ;;



背后的JavaScript的想法是使用JavaScript 属性元素本身的属性,然后拉出来,我们需要的信息 - 名称和属性的值。在属性属性,在现实中,翻出了很多关于各个属性的信息,但我们只想要两个领域。因此,我们得到这两个领域,把它们放到一个字典,然后会的webdriver解析回给我们。通过但是你(这也许可以清理了一下)



这是现在一个词典这样的话你可以循环喜欢。在每对将是的名称的属性,而的每对将是的的属性。



只有网络(这里,谷歌和周围点缀了几个元素测试这一些小的网页),它似乎运作良好。


I want to extract some information from the DOM with Selenium. I'm using the C# WebDriver.

Looking at the IWebElement interface you can easily extract a given attribute. However, I would like to extract all the attributes of an element without knowing their names in before hand.

There must be some simple way of doing this since there is a method for getting an attribute value if you know its name.

An example:

<button id="myButton" ng-click="blabla()" ng-show="showMyButton" 
     some-other-attribute="foo.bar" />



IWebElement element = driver.FindElement(By.Id("myButton"));
Dictionary<string, string> attributes = new Dictionary<string, string>();
// ???????
// Profit.

Hopefully I'm missing something obvious.

Thanks in advance!

解决方案

The .attributes property in JavaScript will return an array of all the attributes a given element has and it's value.

So what you'll need to do is first get a driver that has the capability to run JavaScript:

IJavascriptExecutor javascriptDriver = (IJavaScriptExecutor)driver;

Now, execute it by:

Dictionary<string, object> attributes = javascriptDriver.ExecuteScript("var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;", element) as Dictionary<string, object>;

The idea behind the JavaScript is to use the JavaScript attributes property within the element itself and then pull out the information we need - the name and value of the attribute. The attributes property, in reality, pulls a lot of information about each individual property but we want only two fields. So we get those two fields, put them into a dictionary and WebDriver will then parse it back to us. (It could probably be cleaned up a bit)

It's now a Dictionary and thus you can loop through however you like. The key of each pair will be the name of the attribute, and the value of each pair will be the value of the attribute.

Only tested this with a few elements dotted around the web (here, Google, and a few small web pages) and it seems to work well.

这篇关于让所有的属性从硒的webdriver的IWebElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆