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.
- 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
2 Answers
Reset to default 7 +50The 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.
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.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
版权声明:本文标题:selenium - Locating elements in Protractor vs directly in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745046870a2639420.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论