admin管理员组

文章数量:1410682

In one of the tests, I need to scroll into view of an element which can be done via scrollIntoView() method parameterizing the script with an element located via Protractor:

var elm = element(by.id("myid"));
browser.executeScript("arguments[0].scrollIntoView();", elm.getWebElement());

But, we can also find the element directly via getElementById():

browser.executeScript("document.getElementById('myid').scrollIntoView();");

What is the difference between the two approaches?

The scrollIntoView() is chosen for sample purposes only. The logic inside the script can be more plex.

In one of the tests, I need to scroll into view of an element which can be done via scrollIntoView() method parameterizing the script with an element located via Protractor:

var elm = element(by.id("myid"));
browser.executeScript("arguments[0].scrollIntoView();", elm.getWebElement());

But, we can also find the element directly via getElementById():

browser.executeScript("document.getElementById('myid').scrollIntoView();");

What is the difference between the two approaches?

The scrollIntoView() is chosen for sample purposes only. The logic inside the script can be more plex.

Share edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jul 17, 2016 at 13:09 alecxealecxe 475k127 gold badges1.1k silver badges1.2k bronze badges 2
  • It's no diff, just one of the many ways javascript can do stuff. The first one takes the element as an argument. The second one finds the element. – Gurey Commented Jul 17, 2016 at 14:59
  • Well for first you will have the control of finding the elements(multiple ways) using selenium. For the second you have to use s alone – Madhan Commented Jul 17, 2016 at 17:52
Add a ment  | 

2 Answers 2

Reset to default 7 +50

The first one will tell you explicitly when the element is missing while the second one will raise a JavaScript error saying that null doesn't have the .scrollIntoView method. So to keep the second one maintainable, you need to implicitly handle the case when the element is missing and raise an error with an appropriate message.

The first one is exposed to an unexpected/stale state. The element found by element(by.id("myid")) could be removed from the page just before executing browser.executeScript(). The second one is not exposed to this issue since the page is not updated while the script is executed.

The second one is less expensive since it executes one Selenium mand (ExecuteScript), while the first one executes two (FindElement and ExecuteScript). Sending a Selenium mand to the browser is relatively expensive (minimum of 25ms) and might bee significant with multiple calls.

Both will produce the exact same result and will end up calling the same API on the browser side.

A couple things e to mind.

  1. Maintenance going forward. What happens if your .scrollIntoView() element locator changes? I think I would prefer finding the element using Selenium instead of JS. Reduced chance to have typos, type checking, and so on with an IDE but the IDE won't look into the string that contains your JS.

  2. Selenium accessibility. Since Selenium can't see invisible elements, it could affect your choice either way. Would you want an exception to be thrown if you are trying to scroll to an invisible element? Selenium would let you know where JS wouldn't. Maybe you want to intentionally scroll to an invisible element. Finding invisible elements is a job for JS but not Selenium.

There may be more but this is all I can think of off the top of my head.

本文标签: seleniumLocating elements in Protractor vs directly in JavaScriptStack Overflow