Getting an (d3) element's property that is in

2019-08-12 12:12发布

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.

Chrome console screenshot

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).

Any suggestions?

1条回答
相关推荐>>
2楼-- · 2019-08-12 12:46

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

Two scenarios available:

  • page_source() property

  • innerHtml attribute

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

or

innerHtml

driver.get(url)
time.sleep(3)
elem = self.driver.find_element_by_xpath("//*")
source = elem.get_attribute("innerHTML")
print source
查看更多
登录 后发表回答