Time for action - finding elements by accessing the DOM via JavaScript - Selenium

There are times where the DOM will be updated via AJAX and this means that our locator needed for the test will need some form of JavaScript to see if it is there. In JavaScript, calling the DOM to find the first link on the page would look like document.links[0];. document represents the HTML document and links is an array on that object. On the Chapter (Locators) page of the website, it will show the link that we used in the previous section of this chapter.

But normally it will just be calls to the DOM to see if an element has been added like in the following screenshot:

What just happened?

We have just seen that we can use JavaScript to find elements on the page. This can be extremely useful if you have a web application that does a lot of interaction with the DOM.

Pop quiz - finding Elements with DOM JavaScript

  1. If you wanted to use JavaScript to find the element on the page, which strategy would you use to find it?

    1. ID

    2. Name

    3. DOM

    4. CSS Selector

    5. XPath

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Selenium Topics