17

I am planning a tool in Java which would have a drop down containing all the elements of a web page. Is there any way I can read those into a data structure?

4
  • Hi.. are you able to develop the above tool?? Even am planning to develop the same tool.. need your inputs please.. Commented Oct 13, 2014 at 12:42
  • It's still a WIP, but we are progressing. Commented Oct 14, 2014 at 16:46
  • Can you explain the summary of developing procedure?? Commented Oct 15, 2014 at 9:16
  • 1
    It's a swing UI to develop scripts which invokes a javascript to execute those. Commented Oct 22, 2014 at 6:11

2 Answers 2

30

Yes, there is a way.

Here is some pseudo-code:

List<WebElement> el = driver.findElements(By.cssSelector("*"));

for ( WebElement e : el ) {
  add(e.tagName());
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I'm sure I don't see the cssSelectors in this case. For instance, I tried for google.com, and I get these- Images Maps Play YouTube News Gmail More Sign in
Any idea how to capture custom elements? Have tried using execute-script document.all but that still seems to return only standard elements
Will this also get the custom elements(elements/tags that are not default but created by the user, for example, <my-tag></my-tag>)?
yes it will. any Dom element. By.cssSelector("my-tag")
1

non-pseudo C# version of above: (although I'm just displaying the results in a console

IReadOnlyCollection<IWebElement> el = driver.FindElements(By.CssSelector("*"));
foreach (IWebElement elm in el)
{
    Console.WriteLine(elm.TagName + elm.Text);
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.