使用Selenium获取(DOM)而不是HTML中的(d3)元素的属性 [英] Getting an (d3) element's property that is in DOM, not in the HTML, with Selenium

查看:424
本文介绍了使用Selenium获取(DOM)而不是HTML中的(d3)元素的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用硒(java API)通过d3.js可视化来自动测试页面.我需要在图表(svg)中找到一些数据点. Chrome控制台的屏幕截图可能是解释我的问题的最清晰的方法: 我的元素具有HTML中没有的数据属性(__data__.key).

I am using selenium (java API) to automate testing of a page with a d3.js visualization. I need to find some data point in a chart (svg). The screenshot of my Chrome console is probably be the clearest way to explain my problem: My element has a data property (__data__.key) that is not in HTML.

在JavaScript中,我可以简单地通过<element>.__data__.key获得该属性. 但是在Selenium中,我无法检索它(例如,使用getProperty).

In JavaScript, I can simply get the property by <element>.__data__.key. But in Selenium, I wasn't able to retrieve it (e.g. by using getProperty).

有什么建议吗?

推荐答案

我将使用python解决此问题,但javascript也提供类似的方法.

I will solve it using python but similar methods are available in javascript too.

有两种方案:

  • page_source()属性

innerHtml属性

如果您在正确的时间访问DOM,则这两种方法都会与DOM交互,而DOM随后会通过AJAX加载.一旦获得页面的DOM源,就可以实施所需的检查.

Both approaches interact with DOM that is loaded later on via AJAX, provided you access it at the right time. Once you get the DOM source of the page you can implement the checks you need.

page_source

driver.get(url)
time.sleep(3)
source = driver.page_source
print source

innerHtml

driver.get(url)
time.sleep(3)
elem = self.driver.find_element_by_xpath("//*")
source = elem.get_attribute("innerHTML")
print source

这篇关于使用Selenium获取(DOM)而不是HTML中的(d3)元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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