Selenium Webdriver获取元素的所有数据属性 [英] Selenium webdriver get all the data attributes of an element

查看:1506
本文介绍了Selenium Webdriver获取元素的所有数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Selenium Webdriver,我具有以下元素

Using Selenium webdriver I have the following element

<div data-1223="some data" data-209329="some data" data-dog="some value">

有没有一种方法可以获取元素的所有数据属性? 对于特定属性,我可以使用

Is there a way to get all the data- attributes of the element? For a specific attribute I can use

elem.getAttribute('data-1223') 

但是如何将所有数据属性汇总在一起

but how can I get all the data attributes together

推荐答案

在下面的代码中使用:-

Use below Code:-

WebElement element = "Your Element"; // Your element
JavascriptExecutor executor = (JavascriptExecutor) driver;
Object aa=executor.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);
System.out.println(aa.toString());

希望它会对您有所帮助:)

Hope it will help you :)

这篇关于Selenium Webdriver获取元素的所有数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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