1

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 Answer 1

0

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
5
  • 1
    I'm looking for something that is in the DOM, but not in the HTML. The pageSource and innerHTML, both only gives me the HTML.
    – Len
    Feb 23, 2015 at 15:29
  • e.g. if I would try to get the innerHTML of the element I selected in my screenshot, it returns nothing (because there is no html inside that element).
    – Len
    Feb 23, 2015 at 15:33
  • 1
    The solution has been tested on bl.ocks.org/erikvullings/raw/51cc5332439939f1f292 the results of selenium (same for both scenarios) is pastebin.com/mkFEPnNY In the result page you have all the data inside the chart object
    – aberna
    Feb 23, 2015 at 15:42
  • Thanks, I've successfully confirmed your example with getPageSource() works (in Java). (I couldn't get innerHTML to work, which returns an empty string. ) Unfortunately, similar code with a getPageSource on my webpage (which I can not make available) does not return any svg in it. I've no clue why (or why it does in your case).
    – Len
    Feb 23, 2015 at 16:13
  • well.. there could be a lot of reason for it but without enough info this problem is as a Needle in a haystack.
    – aberna
    Feb 23, 2015 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.